Tom貓的動畫

今天我們來學(xué)一下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播放一組動態(tài)圖片

//1.準(zhǔn)備一組圖片對象(初始化數(shù)組)

var imageArray:[UIImage] = Array()

for i in 0...25 {

//準(zhǔn)備圖片名

let imageName = String(format: "angry_%02d.jpg", i)

//初始化圖片對象

let image = UIImage(named: imageName)

//將圖片放進(jìn)數(shù)組中

imageArray.append(image!)

}

//給播放的動畫賦值

aImageView.animationImages = imageArray

//2.設(shè)置動畫持續(xù)時間

aImageView.animationDuration = 2.0

//3.設(shè)置動畫重復(fù)次數(shù)

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:.

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容