swift -> UIScrollView 实现 滑动 滚动

下面的代码 实现 横向 滚动 , 纵向的话  同理,

        
        //该例子 总归 4个 页面
        //每个分页的宽度
        let each_width:CGFloat = 100;
        //高度
        let frame_height:CGFloat = 80;
        
        
        let scroll = UIScrollView();
        scroll.backgroundColor = UIColor.gray
        //只显示出来的区域
        scroll.frame = CGRect(x: 40, y: 50, width: each_width, height: frame_height);
        //总共4个分页加起来的区域 该 区域大于 frame
        scroll.contentSize = CGSize(width: each_width*4, height: frame_height);
        //是否支持分页
        scroll.isPagingEnabled = true
        //初始位置,可以理解成默认从第几个页面开始
        scroll.contentOffset = CGPoint(x: 0, y: 0);
        
        //scroll.contentInset = UIEdgeInsets(top: 10,left: 10,bottom: 10,right: 10) //内边距
        
        
        
        let imageView =  UIView(frame: CGRect(x:0, y:0, width:each_width, height:frame_height))
        
        imageView.backgroundColor = UIColor.red;
        
        scroll.addSubview(imageView)
        
        
        
        let imageView1 =  UIView(frame: CGRect(x:each_width*1, y:0,width:each_width, height:frame_height))
        
        imageView1.backgroundColor = UIColor.blue;
        
        scroll.addSubview(imageView1)
        
        
        
        let imageView2 =  UIView(frame: CGRect(x:each_width*2, y:0,width:each_width, height:frame_height))
        
        imageView2.backgroundColor = UIColor.gray;
        
        scroll.addSubview(imageView2)
        
        
        
        let imageView3 =  UIView(frame: CGRect(x:each_width*3, y:0,width:each_width, height:frame_height))
        
        imageView3.backgroundColor = UIColor.yellow
        
        scroll.addSubview(imageView3)
        
        self.view.addSubview(scroll)

猜你喜欢

转载自mft.iteye.com/blog/2368271
今日推荐