Transform动画初解 in Swift

简介:

创建一个界面,就像这样的:

顶部是一个UISegmentControl,用来制定transform的类型。分别是:CGAffineTransformMakeTranslation、CGAffineTransformTranslateCGAffineTransformIdentity.

然后是一个UILabel,这个Label实时的显示当前的动画类型是什么。

桔色的是动画的View。

最下面是一个按钮,按这个按钮桔色的View开始执行动画。

其他的,蓝色的线就是这几个View的Constraints。指定这几个view的定位是如何的,比如,相对于顶部的距离,相对于左边的距离,右边的距离等。

配置好页面上的View之后,给这些View在Controller中指定对应的对象。并在nib文件中指定各个View的事件。

View在controller中对应的对象:

    @IBOutlet weak var animationView: UIView!
    @IBOutlet weak var animationLabel: UILabel!

UISegmentControl的事件:

    @IBAction func segmentAction(sender: AnyObject) {
        var segmentControl = sender as UISegmentedControl
        animationType = segmentControl.selectedSegmentIndex
    }

运行动画的按钮的事件:

复制代码
@IBAction func runAction(sender: AnyObject) {
        var distance: CGFloat = 30
        switch animationType {
        case 0:
            self.animationLabel.text = "CGAffineTransformMakeTranslation"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformMakeTranslation(distance, 0)
            })
        case 1:
            self.animationLabel.text = "CGAffineTransformTranslate"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(self.animationView.transform, distance, 0)
            })
        case 2:
            self.animationLabel.text = "CGAffineTransformIdentity"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, distance, 0)
            })
        default:
            println("")
        }
    }
复制代码

 

ViewDidLoad设置选择的动画的类型,这里我们不直接取UISegmentControl的selectedIndex的值。所以,在ViewDidLoad方法内设定默认值是1(即选定到是第一个)。

全部代码:

复制代码
//
//  ViewController.swift
//  TransformDemo
//
//  Created by Bruce Lee on 30/11/14.
//  Copyright (c) 2014 Dynamic Cell. All rights reserved.
//  
//  QQ:1828099940, 群:58099570 欢迎加入讨论
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var animationView: UIView!
    @IBOutlet weak var animationLabel: UILabel!
    
    var animationType: Int!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        animationType = 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func segmentAction(sender: AnyObject) {
        var segmentControl = sender as UISegmentedControl
        animationType = segmentControl.selectedSegmentIndex
    }

    @IBAction func runAction(sender: AnyObject) {
        var distance: CGFloat = 30
        switch animationType {
        case 0:
            self.animationLabel.text = "CGAffineTransformMakeTranslation"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformMakeTranslation(distance, 0)
            })
        case 1:
            self.animationLabel.text = "CGAffineTransformTranslate"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(self.animationView.transform, distance, 0)
            })
        case 2:
            self.animationLabel.text = "CGAffineTransformIdentity"
            UIView.animateWithDuration(1.0, animations: {
                self.animationView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, distance, 0)
            })
        default:
            println("")
        }
    }
}
复制代码

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/p/4133266.html ,如需转载请自行联系原作者
相关文章
|
Swift Perl
Swift:Lottie实现tabBar切换动画
Swift:Lottie实现tabBar切换动画
600 0
Swift:Lottie实现tabBar切换动画
|
Swift
Swift学习笔记(1)过渡动画(CATransition和UIViewAnimation)的用法
Swift学习笔记(1)过渡动画(CATransition和UIViewAnimation)的用法 CATransition和UIViewAnimation是场景切换时常用的两种过渡动画 目录 Swift学习笔记1过渡动画CATransition和UIViewAnimation的用法 目录 CATransition CATransition的type属性
2680 0
|
iOS开发
swift4.0 CAKeyframeAnimation,抖动动画
demo地址: https://github.com/weiman152/CAKeyframeAnimationDemo 继上一篇之后,这一篇我们来介绍使用CAKeyframeAnimation来实现简单的抖动动画。
1443 0
|
iOS开发
swift4.0 CAKeyframeAnimation动画使用
简单的平移、缩放和旋转以及渐隐渐显,都可以使用 UIView.animate(withDuration: 0.25, animations: { // 最终的结果显示 }) { (_) in // 动画完成之后要进行的操作 } 如果是稍微复杂的动画,比如沿着曲线运动,就可以考虑使用关键帧动画CAKeyframeAnimation实现。
1179 0
太阳升起并下落的小动画-SWIFT
一个小小的动画,太阳公公上山又下山。先上效果图。   用 lipecap 录的gif效果有点卡顿。好吧,说下如何实现的。 首先在一个大圆内先计算出内切九边形各个顶点的位置,接着连接相应的顶点变成一个九角星太阳的九条光芒,然后在九角星的中心画一个圈形的Layer,这样就大致画好了大阳的形状。
2708 0

相关课程

更多