
最近发现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(,self.view.frame.size.height, self.view.frame.size.width, );
popView.backgroundColor=UIColor.redColor();
self.view.addSubview(popView); clkbtn=UIButton();
clkbtn.frame=CGRectMake(self.view.frame.size.width/-, self.view.frame.size.height/-, , );
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(,self.view.frame.size.height-, self.view.frame.size.width, );
}, completion: nil);
}else{
display=false;
clkbtn.setTitle("弹出", forState: UIControlState.Normal);
UIView.animateWithDuration(0.5, animations: {
self.popView.frame=CGRectMake(,self.view.frame.size.height, self.view.frame.size.width, );
}, completion: nil);
}
}
}