首页 > 移动平台 > 详细

iOS Swift最简单的Animation

时间:2015-10-24 11:24:14      阅读:417      评论:0      收藏:0      [点我收藏+]

最近发现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

iOS Swift最简单的Animation

原文:http://www.cnblogs.com/rayshen/p/4906414.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!