第七周Swift总结

##day one 先建立scrollView对象(大小先self.view.bounds) 然后添加imageView上去 最后改变scollView大小 一般为self.view.bounds的宽和高的倍数 方法有几个比较常用的 contenSize(确定容量大小) contenOffset 内容偏移量 pagingEnabled(是否分页) directionLocked(锁定方向 每次滑动只能一个方向)

以下是几个方法使用

       //==========从UIView继承下来的属性==========
        //1.创建UIScrollView对象
        //这个时候UIScrollView内容的大小和scrollView本身是一样的
        let scrollView = UIScrollView.init(frame: self.view.bounds)
        //2.添加到界面上
        self.view.addSubview(scrollView)
        //3.设置背景颜色
        scrollView.backgroundColor = UIColor.yellowColor()
        //4.添加imageView
        //imageView的坐标是(0,0),大小是图片的大小
        /*
        let imageView = UIImageView.init(image: UIImage.init(named: "1092.jpg"))
        //在scrollView上添加子视图,实质是将子视图添加到它的内容上的
        scrollView.addSubview(imageView)
         */
        
        //创建多张图片
        for item in 1...4 {
            let imageView = UIImageView.init(frame: CGRectMake(CGFloat(item-1)*scrollView.bounds.width, 0, scrollView.bounds.width, scrollView.bounds.height))
            scrollView.addSubview(imageView)
            imageView.image = UIImage.init(named: "35_\(item).jpg")
        }
        
        
        
        //=========UIScrollView专有属性======
        //核心属性
        //!!!1.内容大小
        //scrollView.contentSize = CGSizeMake(self.view.bounds.width * 2, self.view.bounds.height*2)
        //如果想要能够通过滚动将图片内容显示全,就将scrollView的内容大小设置为和图片一样的大小
        scrollView.contentSize = CGSizeMake(4*scrollView.bounds.width, scrollView.bounds.height*3)
        
        //!!!2.内容偏移量
        //scrollView默认显示的是内容左上角的部分
        scrollView.contentOffset = CGPointMake(scrollView.bounds.width, 0)
        
        //3.设置是否可以分页
        //true->每次滚动的距离是一页的距离
        //一页的大小就是scrollView的frame的大小。如果分页,那么每次横向滚动滚动的距离是scrollView的宽度,上下滚动的距离是scrollView的高度
        scrollView.pagingEnabled = true
        
        
        //其他属性
        //1.内容边距
        //设置scrollView内容到scrollView本身上左下右的距离(默认都是0)
        //scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20)
    
        //3.设置是否锁定一定方法(默认false->同一时间既可以上下滚动又可以左右滚动)
        scrollView.directionalLockEnabled = true  //true->同一时间只能上下滚动或者左右滚动
        
        //3.滑到内容边缘的时候是否可以滚动一段距离再弹回去(默认true)
        scrollView.bounces = false
        
        //4.设置是否可以滚动(默认true->可以滚动)
        scrollView.scrollEnabled = true
        
        //5.设置是否显示滚动条(默认true->显示)
        //是否显示水平方向的滚动条
        scrollView.showsHorizontalScrollIndicator = true
        //是否显示垂直方向的滚动条
        scrollView.showsVerticalScrollIndicator = true
        
        //6.设置滚动条到上下左右的边距
        //注意:这个结构体中的值只有下和右有效。下->水平方向的滚动条。右->垂直方向的滚动条
        scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 100, 10)
        
        //7.设置滚动条的风格
        scrollView.indicatorStyle = .Black
        
        //8.滚动到指定区域(让指定区域可见)
        //scrollView.scrollRectToVisible(CGRectMake(1000, 100, 100, 100), animated: true)
        
        
        //9.是否滚动到顶部(点击状态栏是否可以自动回到顶部)
        scrollView.scrollsToTop = true
        
复制代码

确定缩放对象


        //7.通过带动画效果的方式设置偏移量
        //self.scrollView.setContentOffset(CGPointMake(self.scrollView.bounds.width, 0), animated: true)
        
        //8.设置缩放倍数
        self.scrollView.maximumZoomScale = 3
        self.scrollView.minimumZoomScale = 0.5

    //2.告诉UIScrollView在缩放的时候,对哪个视图进行缩放(设置缩放对象)
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?{
    
        return scrollView.subviews[0]
    }
    
    
复制代码

以下是代理



        //6.设置代理
        self.scrollView.delegate = self

extension ViewController: UIScrollViewDelegate{

    //============滚动相关============
    //1.在scrollView滚动的时候实时调用
    func scrollViewDidScroll(scrollView: UIScrollView){
    
        print("正在滚动")
        print(scrollView.contentOffset)
    }
    
    //2.在scrollView将要开始拖动的时候调用
    func scrollViewWillBeginDragging(scrollView: UIScrollView){
    
        print("将要开始拖拽")
    }
    
    //3.停止减速的时候会调用(实质就是停止滚动的时候会调用)
    func scrollViewDidEndDecelerating(scrollView: UIScrollView){
    
        print("停止减速")
    }
    
    
    //4.将要停止拖拽的时候调用(手将要松开的时候,不代表停止滚动)
    //参数2:手松开时候在x方向上和在y方向的速度
    //参数3:预判的滚动停止时候的偏移量
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){
    
        print("将要停止拖拽")
        print(targetContentOffset)
    }
    
    
    //5.手将要停止拖拽的时候调用
    func scrollViewWillBeginDecelerating(scrollView: UIScrollView){
    
        print("将要开始减速")
    }
    
    //6.已经停止滚动动画的时候调用
    //只有在通过setContentOffset的方法使用动画效果让scrollView滚动,在停止滚动的时候才会调用这个方法
    func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView){
        
        print("停止滚动动画")
    }
    
    
    //7.只有在通过点击状态栏自动滚动到顶部的时候才会调用
    func scrollViewDidScrollToTop(scrollView: UIScrollView){
    
        print("已经滚动到顶部")
    }
    
    
    //============缩放相关=============
    //UIScrollView已经实现了缩放功能
    //想要让UIScrollView上的内容进行缩放必须满足以下条件:
    //a.设置缩放的最大倍数和最小倍数(默认都是1)
    //b.通过协议方法告诉UIScrollView缩放对象
    //1.正在缩放的时候会实时调用
    func scrollViewDidZoom(scrollView: UIScrollView){
    
        print("正在缩放")
    }
复制代码

一般UIScrollView结合前面滚动停止的函数获取当前是第几页(讲偏移的x值除以其宽度)

//MARK: - ScrollView Delegate
extension ViewController: UIScrollViewDelegate{

    //停止滚动
    func scrollViewDidEndDecelerating(scrollView: UIScrollView){
    
        //拿到当前是第几页
        let page = scrollView.contentOffset.x / scrollView.bounds.width
        
        //更新pageControl
        self.pageControl?.currentPage = Int(page)
    }
}

//MARK: - PageControl
extension ViewController{

    func creatPageControl() {
        
        //1.创建UIPageControl对象
        self.pageControl = UIPageControl.init(frame: CGRectMake(0, 230, Screen_W, 20))
        //2.添加到界面上
        self.view.addSubview(self.pageControl!)
        //3.设置背景颜色
        //self.pageControl?.backgroundColor = UIColor.yellowColor()
        
        //4.核心属性
        //设置总的页数
        self.pageControl?.numberOfPages = self.scrollView.subviews.count
        //设置当前页(从0开始)
        self.pageControl?.currentPage = 0
        
        //5.设置圆点的颜色
        self.pageControl?.pageIndicatorTintColor = UIColor.yellowColor()
        self.pageControl?.currentPageIndicatorTintColor = UIColor.redColor()
        
        //6.添加事件
        self.pageControl?.addTarget(self, action: "pageControlAction:", forControlEvents: .ValueChanged)
    }
    
    //值改变事件发生调用的方法
    func pageControlAction(pageControl:UIPageControl) {
        
        let offsetX = CGFloat(pageControl.currentPage) * Screen_W
        self.scrollView.setContentOffset(CGPointMake(offsetX, 0), animated: true)
    }
}
复制代码

##day two 这里主要讲的是UITableView的一些用法 主要是两个 1.方法 设置行高 self.tableView.rowHeight = 150 头文件头像 self.tableView.tableHeaderView = imageView 分割线       tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10)                  //设置分割线的风格         //None -> 没有分割线         tableView.separatorStyle = .SingleLine                   //设置分割线的颜色         tableView.separatorColor = UIColor.redColor() 2.代理的一些方法 主要有两个代理 1.DataSource 就是对行数和组数的设定 还有行数里面添加的内容图片 对行数有多少行设定 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 对组数有多少组设定 func numberOfSectionsInTableView(tableView: UITableView) -> Int 创建cell 判断cell是否有重复的 用id进行判断 然后在对数据进行刷新 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  设置每个分组的题目 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? 这里是设置索引栏 就是右边出现一个索引 然后点击相应的地方进行跳转 func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? 2.Delegate 就是对组数和行数的高度设定

设定cell的高度 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

设置header和footer的高度

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 50
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0
    }
复制代码

设定每个分组的headerView的大小坐标颜色 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

注意: 这里还有一个重要的方法 选择相应的行数或者组数进行跳转 跳转到下一个页面 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值