swift基础之UIAnimation 动画(手势操作)

前端之家收集整理的这篇文章主要介绍了swift基础之UIAnimation 动画(手势操作)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit

class 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.transform = CGAffineTransformMakeScale(-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.transform = CGAffineTransformIdentity;
    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!
    })
}

}


要准备四张图片



运行结果界面

当点击时会跳跃,当向右滑动会向右走,左划想左走

原文链接:https://www.f2er.com/swift/323511.html

猜你在找的Swift相关文章