首页 技术 正文
技术 2022年11月15日
0 收藏 776 点赞 3,808 浏览 2689 个字

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

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 =
switch animationType {
case :
self.animationLabel.text = "CGAffineTransformMakeTranslation"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformMakeTranslation(distance, )
})
case :
self.animationLabel.text = "CGAffineTransformTranslate"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformTranslate(self.animationView.transform, distance, )
})
case :
self.animationLabel.text = "CGAffineTransformIdentity"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, distance, )
})
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 UIKitclass ViewController: UIViewController { @IBOutlet weak var animationView: UIView!
@IBOutlet weak var animationLabel: UILabel! var animationType: Int! override func viewDidLoad() {
super.viewDidLoad() animationType =
} 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 =
switch animationType {
case :
self.animationLabel.text = "CGAffineTransformMakeTranslation"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformMakeTranslation(distance, )
})
case :
self.animationLabel.text = "CGAffineTransformTranslate"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformTranslate(self.animationView.transform, distance, )
})
case :
self.animationLabel.text = "CGAffineTransformIdentity"
UIView.animateWithDuration(1.0, animations: {
self.animationView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, distance, )
})
default:
println("")
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905