Facebook開源動畫庫 POP-小執行個體,facebookpop-
執行個體1:圖片視圖跟著手在螢幕上的點改變大小
- (void)viewDidLoad{ [super viewDidLoad]; //添加手勢 UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] init]; [gesture addTarget:self action:@selector(changeSize:)]; [self.view addGestureRecognizer:gesture]; }- (void)changeSize:(UIPanGestureRecognizer*)tap{ POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; CGPoint point = [tap locationInView:self.view]; springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)]; //彈性值 springAnimation.springBounciness = 20.0; //彈性速度 springAnimation.springSpeed = 20.0; [_springView pop_addAnimation:springAnimation forKey:@"changeframe"];}
執行個體2:實現一個彈出收縮視圖的效果,彈出來有彈性的效果,收縮有變小的效果
- (void)viewDidLoad{ [super viewDidLoad]; _showPosition = CGRectMake(320-147, 5, 147, 160); _hidePosition = CGRectMake(320, 5, 0, 0); _popView = [[UIImageView alloc] initWithFrame:_hidePosition]; _popView.image = [UIImage imageNamed:@"menu.png"]; [self.view addSubview:_popView]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(showPop)]; //讓螢幕從導覽列下開始算(0,0) if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { self.edgesForExtendedLayout = UIRectEdgeNone; }}- (void)showPop{ if (_isOpened) { [self hidePop]; return; } _isOpened = YES; POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; positionAnimation.fromValue = [NSValue valueWithCGRect:_hidePosition]; positionAnimation.toValue = [NSValue valueWithCGRect:_showPosition]; positionAnimation.springBounciness = 15.0f; positionAnimation.springSpeed = 20.0f; [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"];}- (void)hidePop{ POPBasicAnimation *positionAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame]; positionAnimation.fromValue = [NSValue valueWithCGRect:_showPosition]; positionAnimation.toValue = [NSValue valueWithCGRect:_hidePosition]; [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"]; _isOpened = NO;}
執行個體3:建立兩個按鍵,增加兩個動畫效果,其中一個按鍵是動畫改變大小,另外一個修改ViewFrame,只要定位好座標跟大小可以做出很不錯的動畫
@interface ViewController ()@property (nonatomic, retain) UIView *button;@property (nonatomic, retain) UIView *popOut;@property (readwrite, assign) BOOL timerRunning;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; _popOut = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TimerPopOut"]]; [_popOut setFrame:CGRectMake(245, 70, 0, 0)]; [self.view addSubview:_popOut]; _button = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TimerButton"]]; [_button setFrame:CGRectMake(240, 50, 46, 46)]; [self.view addSubview:_button]; _timerRunning = NO; [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(demoAnimate:)]];}- (void)demoAnimate:(UITapGestureRecognizer*)tap{ _timerRunning = !_timerRunning; POPSpringAnimation *buttonAnimation = [POPSpringAnimation animation]; buttonAnimation.property = [POPAnimatableProperty propertyWithName:kPOPLayerSize]; if (_timerRunning) { buttonAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(37, 37)]; } else { buttonAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(46, 46)]; } buttonAnimation.springBounciness = 10.0; buttonAnimation.springSpeed = 10.0; [_button pop_addAnimation:buttonAnimation forKey:@"pop"]; POPSpringAnimation *popOutAnimation = [POPSpringAnimation animation]; popOutAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewFrame]; if (!_timerRunning) { popOutAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(245, 70, 0, 10)]; } else { popOutAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(180, 60, 75, 26)]; } popOutAnimation.velocity = [NSValue valueWithCGRect:CGRectMake(200, 0, 300, -200)]; popOutAnimation.springBounciness = 10.0; popOutAnimation.springSpeed = 10.0; [_popOut pop_addAnimation:popOutAnimation forKey:@"slide"];}