ios动画编程
//
// ViewController.swift
// iOS 动画编程
//
// Created by Macx on 18/9/16.
// Copyright (c) 2018年 learn. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//绑定myview视图
@IBOutlet weak var myview: UIView!
@IBOutlet weak var myview2: UIView!
@IBOutlet weak var myview3: UIView!
@IBOutlet weak var mywebview: UIWebView!
var mOriginFrame = CGRect() //记录位置
var mOriginFrame2 = CGRect()
var mOriginFrame3 = CGRect()
override func viewDidLoad() {
super.viewDidLoad()
self.mOriginFrame = self.myview.frame
self.mOriginFrame2 = self.myview2.frame
self.mOriginFrame3 = self.myview3.frame
mywebview.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!))
}
//start按钮
@IBAction func start(sender: AnyObject) {
/*
})
*/
self.optionAnimate()
self.OPerateSubview()
}
//fallback按钮
@IBAction func fallback(sender: AnyObject) {
self.myview.frame = mOriginFrame
self.myview2.frame = mOriginFrame2
self.myview3.frame = mOriginFrame3
self.myview.layer.removeAllAnimations()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//设置start的操作 block方法
func optionAnimate(){
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
self.myview.frame.origin.x += 100
//内层动画属性设置,重写父类方法
UIView.animateWithDuration(10, delay: 0, options:UIViewAnimationOptions.OverrideInheritedDuration | UIViewAnimationOptions.OverrideInheritedCurve |
//设置曲线
UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
self.myview2.frame.origin.x += 100
}, completion: { (finish) -> Void in
print("inner animate finished")
})
}) { (finish) -> Void in
print("outnner animate finished")
}
}
//设置start的操作 beginCommitAnimate方法
func beginCommitAnimate(){
//声明动画开始并配置属性
UIView.beginAnimations("testAnimate", context: nil)
UIView.setAnimationDuration(2.0)
UIView.setAnimationRepeatAutoreverses(true)
UIView.setAnimationRepeatCount(7)
UIView.setAnimationDelegate(self)
UIView.setAnimationWillStartSelector("animationDidStart:")
UIView.setAnimationDidStopSelector("animationDidStop:finished:")
//操作视图
self.myview.backgroundColor = UIColor.magentaColor()
//提交动画
UIView.commitAnimations()
}
//Animation 代理方法
override func animationDidStart(anim: CAAnimation!) {
print("Animation start")
}
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
print("Animation stop")
}
//myview3代理方法
func OPerateSubview(){
UIView.transitionWithView(self.myview3, duration: 2, options: UIViewAnimationOptions.TransitionCurlUp | UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> Void in
self.myview3.frame.origin.x += 190
}) { (finish) -> Void in
print("finished")
}
}
}