Facebook开源动画库 POP-小实例

时间:2023-03-08 15:41:23

实例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(, , 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(-, , , );
_hidePosition = CGRectMake(, , , ); _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(, , , )];
[self.view addSubview:_popOut]; _button = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TimerButton"]];
[_button setFrame:CGRectMake(, , , )];
[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(, )];
}
else {
buttonAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(, )];
}
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(, , , )];
}
else {
popOutAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(, , , )];
}
popOutAnimation.velocity = [NSValue valueWithCGRect:CGRectMake(, , , -)];
popOutAnimation.springBounciness = 10.0;
popOutAnimation.springSpeed = 10.0;
[_popOut pop_addAnimation:popOutAnimation forKey:@"slide"];
}