import UIKitclass VIEwController: UIVIEwController { @IBOutlet weak var penguinVIEw: UIImageVIEw! var frames: NSArray? var dIECenter: CGPoint? overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let image1 = UIImage(named:"penguin_walk01")! let image2 = UIImage(named:"penguin_walk02")! let image3 = UIImage(named:"penguin_walk03")! let image4 = UIImage(named:"penguin_walk04")! let frames: [UIImage] = [image1,image2,image3,image4] penguinVIEw.animationDuration = 0.15; penguinVIEw.animationRepeatCount = 2; penguinVIEw.animationImages = frames; //walk right let swipeGestureRight = UISwipeGestureRecognizer(target: self,action: #selector(VIEwController.walkRight(_:))) swipeGestureRight.direction = UISwipeGestureRecognizerDirection.Right vIEw.addGestureRecognizer(swipeGestureRight) //walk left let swipeGestureleft = UISwipeGestureRecognizer(target: self,action: #selector(VIEwController.walkleft(_:))) swipeGestureleft.direction = UISwipeGestureRecognizerDirection.left vIEw.addGestureRecognizer(swipeGestureleft) //jump let tap = UITapGestureRecognizer(target: self,action: #selector(VIEwController.jump(_:))) vIEw.addGestureRecognizer(tap) //longPress let longPress = UILongPressGestureRecognizer(target: self,action: #selector(VIEwController.longPress(_:))) vIEw.addGestureRecognizer(longPress) } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() }func walkleft(send: UIGestureRecognizer) { print("walk left"); //CHECK IF OUT OF SCREEN if (penguinVIEw.center.x < 0.0) { penguinVIEw.center = CGPointMake(vIEw.frame.size.wIDth,penguinVIEw.center.y); } //FliP AROUND FOR WALKING left self.penguinVIEw.@R_403_4154@ = CGAffine@R_403_4154@MakeScale(-1.0,1.0); //START WALK ANIMATION penguinVIEw.startAnimating() //MOVE THE IMAGE VIEW TO left UIVIEw.animateWithDuration(0.6,animations: { () -> VoID in self.penguinVIEw.center = CGPointMake(self.penguinVIEw.center.x - 30,self.penguinVIEw.center.y); })}func walkRight(send: UIGestureRecognizer) { print("walk right"); if (self.penguinVIEw.center.x > self.vIEw.frame.size.wIDth) { self.penguinVIEw.center = CGPointMake(0,self.penguinVIEw.center.y); } self.penguinVIEw.@R_403_4154@ = CGAffine@R_403_4154@IDentity; penguinVIEw.startAnimating() UIVIEw.animateWithDuration(0.6,animations: { () -> VoID in self.penguinVIEw.center = CGPointMake(self.penguinVIEw.center.x + 30,self.penguinVIEw.center.y) })}func jump(send: UIGestureRecognizer) { penguinVIEw.startAnimating() UIVIEw.animateWithDuration(0.25,animations: { () -> VoID in self.penguinVIEw.center = CGPointMake(self.penguinVIEw.center.x,self.penguinVIEw.center.y - 50) },completion: { (finished: Bool) -> VoID in self.jumpBack() })} func jumpBack() { UIVIEw.animateWithDuration(0.25,animations: { () -> VoID in self.penguinVIEw.center = CGPointMake(self.penguinVIEw.center.x,self.penguinVIEw.center.y + 50) })}func longPress(send: UIGestureRecognizer) { UIVIEw.animateWithDuration(0.33,animations: { () -> VoID in self.dIECenter = self.penguinVIEw.center self.penguinVIEw.center = CGPointMake(self.penguinVIEw.center.x,self.vIEw.frame.size.height) },completion: { (finished: Bool) -> VoID in self.longPressBack() })}func longPressBack() { UIVIEw.animateWithDuration(0.25,animations: { () -> VoID in self.penguinVIEw.center = self.dIECenter! })}}
要准备四张图片
运行结果界面
当点击时会跳跃,当向右滑动会向右走,左划想左走
总结以上是内存溢出为你收集整理的swift基础之UIAnimation 动画(手势 *** 作)全部内容,希望文章能够帮你解决swift基础之UIAnimation 动画(手势 *** 作)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)