Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Last active August 29, 2015 14:27
Show Gist options
  • Save lynxerzhang/978b099f31121d6607e5 to your computer and use it in GitHub Desktop.
Save lynxerzhang/978b099f31121d6607e5 to your computer and use it in GitHub Desktop.
swift动画练习
//
// ViewController.swift
// SwiftMotionPrototypeTips
//
// @see http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sliderRectNum: UISlider!
@IBOutlet weak var sliderCountLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/*
UIView.animateWithDuration(1.0, animations: {
coloredSquare.backgroundColor = UIColor.redColor()
coloredSquare.frame = CGRect(x: Int(screenWidth) - squareWidth, y: 120, width: squareWidth, height: squareHeight)
}, completion: { finished in
})
*/
/*
let options = UIViewAnimationOptions.TransitionCrossDissolve | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat
UIView.animateWithDuration(1.0, delay: 2, options: options, animations: {
coloredSquare.backgroundColor = UIColor.redColor()
coloredSquare.frame = CGRect(x: Int(screenWidth) - squareWidth, y: 120, width: squareWidth, height: squareHeight)
}, completion: nil)
*/
}
func createRandomInt(start: Int, _ end: Int) -> Int
{
return Int(arc4random_uniform(UInt32(end - start))) + start
}
func createRGBColor(red: Int, _ green: Int, _ blue: Int) -> UIColor
{
return UIColor(red: CGFloat(red) / 0xFF, green: CGFloat(green) / 0xFF, blue: CGFloat(blue) / 0xFF, alpha: 1.0)
}
@IBAction func clickAnimatedBtn(sender: AnyObject) {
for index in 0..<Int(sliderRectNum.value) {
let squareWidth = createRandomInt(10, 50)
let squareHeight = squareWidth
let yPosition = createRandomInt(20, 120)
println(yPosition)
let coloredSquare = UIView()
coloredSquare.backgroundColor = createRGBColor(255, 102, 0)
coloredSquare.frame = CGRect(x: 0 - squareWidth, y: yPosition, width: squareWidth, height: squareHeight) //50 * 50 y轴坐标包含状态栏
self.view.addSubview(coloredSquare)
let screenRect = UIScreen.mainScreen().bounds //CGRect
let screenWidth = screenRect.size.width //CGFloat
let screenHeight = screenRect.size.height //CGFloat
//typealias NSTimeInterval = Double
let options = UIViewAnimationOptions.CurveLinear
let delay = NSTimeInterval(900 + arc4random_uniform(100)) / 1000
UIView.animateWithDuration(1.0, delay: delay, options: options, animations: {
coloredSquare.backgroundColor = UIColor.redColor()
coloredSquare.frame = CGRect(x: Int(screenWidth), y: yPosition, width: squareWidth, height: squareHeight)
}, completion: { finished in
coloredSquare.removeFromSuperview()
})
}
}
@IBAction func sliderValueChange(sender: AnyObject) {
sliderCountLabel.text = String(Int(sliderRectNum.value))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
@lynxerzhang
Copy link
Author

  1. UIColor中关于r,g,b的取值范围均为0.0 - 1.0
  2. NSTimeInterval即为Double类型的别名

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment