动画很多的属性,如位置,透明度等,开发者可以根据这些属性来自定义自己需要的动画。现在我们来简单实现如何自定义动画,以视图的透明度的变化为例。其他的动画效果可以参考我的其他两篇博客《iOS项目开发实战——视图动画效果》《iOS项目开发实战——实现视图切换动画》。
(1)同样也在Images.xcassets中拖入一张图片,然后在 Main.storyboard中使用一个ImageView控件来包含这张图片。并且绑定控件到代码中。
(2)然后在ViewController中实现代码如下,我简单实现了透明度循环改变的效果:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image: UIImageView!
var isAlpha1:Bool = true
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(image)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
func anim1(){
image.alpha = 0.5 //半透明;
}
func anim2(){
image.alpha = 1.0 //不透明;
}
func complete(v:Bool){
println("Complete")
isAlpha1 = !isAlpha1 //循环改变透明度;
}
if(isAlpha1){
UIView.transitionWithView(image, duration: 1.0, options: UIViewAnimationOptions.TransitionNone, animations: anim1, completion: complete)
}else{
UIView.transitionWithView(image, duration: 1.0, options: UIViewAnimationOptions.TransitionNone, animations: anim2, completion: complete)
}
}
}。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/chenyufeng1991/article/details/47776149