Use of Swift-scrolling view (Uiscrollview)

Source: Internet
Author: User

1, when the picture size exceeds the screen, using Uiscrollview can implement the scroll bar view, that is, the finger touch scrolling screen to easily browse the entire page.

123456 varscrollView=UIScrollView()scrollView.frame=self.view.boundsvarimageView=UIImageView(image:UIImage(named:"bigpic"))scrollView.contentSize=imageView.bounds.size;scrollView.addSubview(imageView);self.view.addSubview(scrollView)

2, gets the x, y coordinates of the scrolling view movement
By Scrollview.contentoffset.x and Scrollview.contentoffset.y we can take the moving offset position

12345678910111213141516171819202122232425262728 import UIKitclass ViewController: UIViewController,UIScrollViewDelegate {        var scrollView:UIScrollView!        override func viewDidLoad() {        super.viewDidLoad()                scrollView=UIScrollView()        //设置代理        scrollView.delegate = self        scrollView.frame=self.view.bounds        var imageView=UIImageView(image:UIImage(named:"ii"))        scrollView.contentSize=imageView.bounds.size;        scrollView.addSubview(imageView);        self.view.addSubview(scrollView)    }        //视图滚动中一直触发    func scrollViewDidScroll(scrollView: UIScrollView) {        println("x:\(scrollView.contentOffset.x) y:\(scrollView.contentOffset.y)")    }       override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}


3, Gesture zoom in and zoom out
To achieve zoom in and out, you need to specify the maximum scale and minimum scale of allowable scaling for Uiscrollview (default is 1.0).
At the same time, the delegate property specifies a delegate class that inherits the Uiscrollviewdelegate protocol and implements the Viewforzoominginscrollview method in the delegate class.
(Note: To test in the simulator, you need to hold down the option key and drag the content)

123456789101112 scrollView.minimumZoomScale=0.1 //最小比例scrollView.maximumZoomScale=3 //最大比例scrollView.delegate=self    func viewForZoomingInScrollView( scrollView: UIScrollView!) -> UIView!{     for subview : AnyObject in scrollView.subviews {      if subview.isKindOfClass(UIImageView) {         return subview as UIView     }   }    return nil}

Use of Swift-scrolling view (Uiscrollview)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.