iOS Swift最简单的Animation

最近发现Animation是一个iOS开发中非常好玩的元素,能给应用的交互性增色不少。比如很多音乐应用的菜单从底部弹出和隐藏的效果。

Animation最核心的当然就是UIView的animateWithDuration这个类方法了,另外有个博客介绍了很多animation的文章也很不错:

http://www.devtalking.com/articles/uiview-animation-practice/

 

念在好久没用swift开发了,于是花了几分钟写了个简单的demo复习下

//
//  ViewController.swift
//  UIAnimationTest
//
//  Created by shen on 15/10/24.
//  Copyright © 2015年 shen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var popView:UIView!
    var clkbtn:UIButton!=UIButton()
    var display:Bool=false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        popView=UIView();
        popView.frame=CGRectMake(0,self.view.frame.size.height, self.view.frame.size.width, 100);
        popView.backgroundColor=UIColor.redColor();
        self.view.addSubview(popView);
        
        clkbtn=UIButton();
        clkbtn.frame=CGRectMake(self.view.frame.size.width/2-30, self.view.frame.size.height/2-20, 60, 40);
        clkbtn.setTitle("弹出", forState: UIControlState.Normal);
        clkbtn.backgroundColor=UIColor.grayColor();
        clkbtn.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview(clkbtn);
        
            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func buttonClicked(sender:UIButton)
    {
        if(display==false){
            display=true;
            clkbtn.setTitle("隐藏", forState: UIControlState.Normal);
            UIView.animateWithDuration(0.5, animations: {
                self.popView.frame=CGRectMake(0,self.view.frame.size.height-100, self.view.frame.size.width, 100);
                }, completion: nil);
        }else{
            display=false;
            clkbtn.setTitle("弹出", forState: UIControlState.Normal);
            UIView.animateWithDuration(0.5, animations: {
                self.popView.frame=CGRectMake(0,self.view.frame.size.height, self.view.frame.size.width, 100);
                }, completion: nil);
        }
    }
}

demo地址: https://github.com/rayshen/SwiftAnimationTest

posted @ 2015-10-24 11:03  Rayshen  阅读(598)  评论(0编辑  收藏  举报