0115iosapp_动画动作方法:位置移动,尺寸变化,重复动作

项目 movemain0114

viewcontroller

实现位置移动,尺寸变化

//
//  ViewController.swift
//  movemore0114
//
//  Created by Mac on 1/14/19.
//  Copyright © 2019 wjb. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    //黄色视图
    @IBOutlet weak var yellowView: UIView!
    //绿色视图
    @IBOutlet weak var greenView: UIView!
    
    //动画效果,黄色做移动,绿色做大小变化
    //绿色视图中心点变量初始化,加载完成后赋值
    var greenView_center :CGPoint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //变量赋值
        greenView_center = greenView.center
    }
    
    //加载动画效果
    override func viewDidAppear(_ animated: Bool) {
        UIView.animate(withDuration: 2, delay: 0, options: [UIView.AnimationOptions.autoreverse,.repeat], animations: {
            //移动Y轴坐标
            self.yellowView.center.y += 500
        }, completion: nil)
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 1, options: [UIView.AnimationOptions.autoreverse,.repeat], animations: {
            
            //改变大小
            self.greenView.frame = CGRect(x: self.greenView_center!.x - 60 , y: self.greenView_center!.y - 60, width: 150, height: 150)
            
            
        }, completion: nil)
        //usingSpringWithDamping: 0.2, initialSpringVelocity: 1
        //UIView.AnimationOptions.autoreverse  重复 ,.repeat 重复
        
    }


}

secondviewcontroller

弹出,关闭,显示

//
//  secondViewController.swift
//  movemore0114
//
//  Created by Mac on 1/14/19.
//  Copyright © 2019 wjb. All rights reserved.
//

import UIKit

class secondViewController: UIViewController {

    @IBOutlet weak var redView: UIView!
    
    //关闭
    @IBAction func closeButton(_ sender: UIButton) {
        //视图透明
        redView.alpha = 0
        
    }
    
    //弹出
    @IBAction func openButton(_ sender: UIButton) {
        //视图不透明
        redView.alpha = 1
        //初始变形
        redView.transform = CGAffineTransform(scaleX: 0.5, y: 2)
        //执行动画方法
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 1, options: UIView.AnimationOptions.curveEaseOut, animations: {
            
            //恢复原始状态 
            self.redView.transform = CGAffineTransform.identity
            
        }, completion: nil)
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //初始,视图透明
        redView.alpha = 0

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

thirsviewcontroller

背景图移动循环

//
//  thirsViewController.swift
//  movemore0114
//
//  Created by Mac on 1/14/19.
//  Copyright © 2019 wjb. All rights reserved.
//

import UIKit

class thirsViewController: UIViewController {

    let bgImageView = UIImageView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        bgImageView.image = UIImage(named: "bg0114")
        bgImageView.frame = CGRect(x: 0, y: 0, width: (bgImageView.image?.size.width)!, height: view.frame.height)
        view.addSubview(bgImageView)
    }
    
    //执行加载完后的动画
    override func viewDidAppear(_ animated: Bool) {
        
        UIView.animate(withDuration: 20, delay: 0, options: [UIView.AnimationOptions.autoreverse,.repeat], animations: {
            
            self.bgImageView.center.x = self.bgImageView.center.x - (self.bgImageView.image?.size.width)! + self.view.frame.width
            
        }, completion: nil)
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

猜你喜欢

转载自blog.csdn.net/whqwjb/article/details/86491435