今天我们来学一下tom猫的动画吧 首先我们先找到一个tom猫的一系列动作。添加到工程中。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//添加一个图片的属性
var imageView:UIImageView!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.8931787501, green: 0.9535736618, blue: 1, alpha: 1)
self.window?.makeKeyAndVisible()
self.window?.rootViewController = UIViewController()
//UIImageView 图片展示控件,因为UIImage本身不具备显示功能,要想展示UIImage必须借助于UIImageView
let aImageView = UIImageView(frame: CGRect(x: 10, y: 10, width: (self.window?.bounds.size.width)! - 20, height: (self.window?.bounds.size.height)! - 20))
aImageView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.8485177922, blue: 0.9226891988, alpha: 1)
//aImageView.image = UIImage(named:"happy.png")
aImageView.image = #imageLiteral(resourceName: "angry_00.jpg")
self.window?.addSubview(aImageView)
//使用UIImageView播放一组动态图片
//1.准备一组图片对象(初始化数组)
var imageArray:[UIImage] = Array()
for i in 0...25 {
//准备图片名
let imageName = String(format: "angry_%02d.jpg", i)
//初始化图片对象
let image = UIImage(named: imageName)
//将图片放进数组中
imageArray.append(image!)
}
//给播放的动画赋值
aImageView.animationImages = imageArray
//2.设置动画持续时间
aImageView.animationDuration = 2.0
//3.设置动画重复次数
aImageView.animationRepeatCount = 1
//4.开始动画
//aImageView.startAnimating()
let angryButton = UIButton(frame: CGRect(x: 157, y: 530, width: 100, height: 100))
angryButton.backgroundColor = UIColor.clear
self.window?.addSubview(angryButton)
angryButton.addTarget(self, action: #selector(angryButtonAction), for: .touchUpInside)
//给属性imageview赋值
self.imageView = aImageView
return true
}
//MARK:- button的点击事件
func angryButtonAction() {
//开始动画
self.imageView.startAnimating()
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}