Swift -> UIScrollView implements sliding scrolling

 

The following code implements horizontal scrolling, and the same is true for vertical scrolling.

 

        
        //The example totals 4 pages
        // width of each pagination
        let each_width:CGFloat = 100;
        //high
        let frame_height:CGFloat = 80;
        
        
        let scroll = UIScrollView();
        scroll.backgroundColor = UIColor.gray
        //Only the displayed area
        scroll.frame = CGRect(x: 40, y: 50, width: each_width, height: frame_height);
        //The total area of ​​4 pages added up is larger than the frame
        scroll.contentSize = CGSize(width: each_width*4, height: frame_height);
        //Whether paging is supported
        scroll.isPagingEnabled = true
        //Initial position, which can be understood as the default starting from the first page
        scroll.contentOffset = CGPoint(x: 0, y: 0);
        
        //scroll.contentInset = UIEdgeInsets(top: 10,left: 10,bottom: 10,right: 10) //Inset margins
        
        
        
        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)

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264254&siteId=291194637