UI Dynamics is a new component of Uikit, which provides physics-related features and animations to the views in iOS. Allows you to introduce force and physical properties into the view, allowing your view to bounce, dance, be affected by gravity, and so on.
The following example demonstrates the use of Uisnapbehavior to add a move adsorption behavior to Uiimageview.
as follows: (click on the screen, the Apple will dynamically move to the location of the click, moving with the tilt and easing effect)
1234567891011121314151617181920212223242526272829303132 |
import UIKit
class ViewController
:
UIViewController {
@IBOutlet weak var imageView:
UIImageView
!
var dynamicAnimator =
UIDynamicAnimator
()
var snap:
UISnapBehavior
?
override func viewDidLoad() {
super
.viewDidLoad()
dynamicAnimator =
UIDynamicAnimator
(referenceView:
self
.view)
}
@IBAction func tapped(sender:
AnyObject
){
//获取点击位置
let tap = sender
as
!
UITapGestureRecognizer
let point = tap.locationInView(
self
.view)
//删除之前的吸附,添加一个新的
if self
.snap !=
nil {
self
.dynamicAnimator.removeBehavior(
self
.snap!)
}
self
.snap =
UISnapBehavior
(item:
self
.imageView, snapToPoint: point)
self
.dynamicAnimator.addBehavior(
self
.snap!)
}
override func didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
Original from: www.hangge.com reprint please keep the original link: http://www.hangge.com/blog/cache/detail_870.html
Swift-Add mobile adsorption behavior to uikit components using UI Dynamics