animation = YES/NO 還在用這個玩意?
?
?
?
動畫看起來都很牛X吧,其實用我們的UIKit庫也可以實現一些動畫的,
雖然不能向CG那么厲害,但是在項目里加上一些小小的動畫,
就瞬間可以讓你的項目高大上不少.
如果之前沒接觸過那也沒關系,先上一張弟弟圖;
平移.gif
再接著上代碼
#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController ()
@property(nonatomic,strong)UIView *translationView;
@property(nonatomic,strong)UITextView *password;
@property(nonatomic,strong)UIButton *login;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btn setTitle:@"go" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor blackColor];
btn.center = self.view.center;
[btn addTarget:self action:@selector(translationDidChange) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn];
self.translationView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 50, 50)];
self.translationView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.translationView];
}
最后觸發(fā)方法 通過改變控件中心點的frame來實現平移
這里面參數UIview直接換成你想要的控件類型;
animateWith 后面的Duration是設置動畫完成的過程用時;
后面block就是位移的目標地點;
-(void)translationDidChange{
CGPoint accountCenter = self.translationView.center;
self.translationView.center = accountCenter;
accountCenter.x += self.view.frame.size.width-50;
[UIView animateWithDuration:3.5 animations:^{
self.translationView.center = accountCenter;
} completion:nil];
}
如果你需要控件平移效果的話,這個很基本了已經
從這個一個方法我們就可以實現上下左右移動
寬高都改變的情況下斜著來也行,block還可以寫透明度
斜向位移加改變透明度
觸發(fā)方法代碼:
CGPoint accountCenter = self.translationView.center;
CGFloat accountAlpha = self.translationView.alpha;
self.translationView.center = accountCenter;
self.translationView.alpha = accountAlpha;
accountCenter.x += self.view.frame.size.width-50;
accountCenter.y += self.view.frame.size.height - 150;
accountAlpha -= 1;
[UIView animateWithDuration:3.5 animations:^{
self.translationView.center = accountCenter;
self.translationView.alpha = accountAlpha;
} completion:nil];
可以實現的效果非常非常多,
比如登陸注冊的時候可以搞個輸入框,密碼框
確定按鈕動畫入場.........漬漬漬.....