最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
苹果开发之一个简单的动画效果(方块来回反复移动)
时间:2015-12-20 编辑:简简单单 来源:一聚教程网
使用 UIView.animateWithDuration() 给方块添加动画,让其在屏幕左侧与右侧间不断地来回运动。
import UIKit
class ViewController: UIViewController {
// 方块
var block:UIView!
override func viewDidLoad()
{
super.viewDidLoad()
//创建方块
block = UIView(frame:CGRectMake(0, 0, 25, 25))
block.center.y = self.view.bounds.height / 2 //方块垂直居中
block.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(block)
//播放动画
playAnimation()
}
//播放动画
func playAnimation()
{
UIView.animateWithDuration(0.6, delay: 0.4, options: [.Repeat, .Autoreverse],
animations: {
self.block.frame.origin.x = self.view.bounds.width - self.block.frame.width
},
completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2,添加多个大小不一的方块,并设置动画
import UIKit
class ViewController: UIViewController {
// 方块
var block1:UIView!
var block2:UIView!
var block3:UIView!
override func viewDidLoad()
{
super.viewDidLoad()
//创建方块
block3 = UIView(frame:CGRectMake(0, 0, 15, 15))
block3.center.y = self.view.bounds.height / 2 //方块垂直居中
block3.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(block3)
block2 = UIView(frame:CGRectMake(0, 0, 20, 20))
block2.center.y = self.view.bounds.height / 2 //方块垂直居中
block2.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(block2)
block1 = UIView(frame:CGRectMake(0, 0, 25, 25))
block1.center.y = self.view.bounds.height / 2 //方块垂直居中
block1.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(block1)
//播放动画
playAnimation()
}
//播放动画
func playAnimation()
{
UIView.animateWithDuration(0.6, delay: 0.4, options: [.Repeat, .Autoreverse],
animations: {
self.block1.frame.origin.x =
self.view.bounds.width - self.block1.frame.width
},
completion: nil)
UIView.animateWithDuration(0.6, delay: 0.45, options: [.Repeat, .Autoreverse],
animations: {
self.block2.frame.origin.x =
self.view.bounds.width - self.block2.frame.width
},
completion: nil)
UIView.animateWithDuration(0.6, delay: 0.5, options: [.Repeat, .Autoreverse],
animations: {
self.block3.frame.origin.x =
self.view.bounds.width - self.block2.frame.width
},
completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
-
下一个: 新浪分期是什么?新浪分期怎么用?
相关文章
- 如何简单的画出苹果 苹果步骤图解教程 05-17
- 利用Photoshop制作简单的水晶苹果壁纸教程 01-17
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22