• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Transform动画初解 in Swift

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

顶部是一个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: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Kotlin 很可能像当年的 Swift,首选 RxJava 而不是 Flutter发布时间:2022-07-13
下一篇:
Swift4.0 监听键盘删除按钮事件发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap