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 |
var scrollView= UIScrollView () scrollView.frame= self .view.bounds var imageView= 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 UIKit
class 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)