大赏iOS画图与动画iOS 开发

iOS中的一些动画。

2016-10-07  本文已影响197人  oriyum

一、CGAffineTransform类有三个动画,很实用,且用起来很简单。

1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)
平移动画:设置平移量
2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
缩放:设置缩放比例。仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。
使用最频繁,常用于点击按钮,按钮放大的效果。
3.CGAffineTransformMakeRotation(CGFloat angle)
旋转:设置旋转角度

利用上面的几个方法,可以写出下面的某个alert的显示隐藏的动画,为了显示明显,我把时间都调整为0.5,适当调整动画时间就可以得到满意的效果。

animation.gif
具体实现如下,补充下有关block用法
block的声明   
 ^(return type)(argument list) {//code block}
block的定义  
 typedef ( return type ) (^ blockNmae) (argument list)

分别定义两个block,显示隐藏动画时候用到。
//block的定义
//typedef ( return type ) (^ blockNmae) (argument list)
typedef void (^CustomAnimationBlock)(void);
typedef void (^CustomCompletionAnimationBlock)(BOOL finished);
显示动画的方法
- (void)show {
    self.bgview.transform = CGAffineTransformMakeScale(FLT_EPSILON, FLT_EPSILON);
  //block的声明 ^(return type)(argument list){//code block}
    CustomAnimationBlock expandBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(1.3f, 1.3f);};
    CustomAnimationBlock identityBlock = ^{self.bgview.transform = CGAffineTransformIdentity;};
    CustomCompletionAnimationBlock completionBlock = ^(BOOL done){[UIView animateWithDuration:0.5f animations:identityBlock];};
    [UIView animateWithDuration:0.5f animations:expandBlock completion:completionBlock];
}
隐藏动画的方法
- (void)dismiss {
    CustomAnimationBlock expandBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(1.1f, 1.1f);};
    CustomAnimationBlock shrinkBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(FLT_EPSILON, FLT_EPSILON);};
    CustomCompletionAnimationBlock completionBlock = ^(BOOL done){[UIView animateWithDuration:0.5f animations:shrinkBlock];};
    [UIView animateWithDuration:0.5f animations:expandBlock completion:completionBlock];
}

代码下载地址

二、补充

 UIRectEdgeNone = 0, 
UIRectEdgeTop = 1 << 0, 
UIRectEdgeLeft = 1 << 1, 
UIRectEdgeBottom = 1 << 2, 
UIRectEdgeRight = 1 << 3,
 UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
- (void)wiggle{
 [UIView animateWithDuration:0.25f animations:^(){
 pullImageView.center = CGPointMake(pullImageView.center.x,
 pullImageView.center.y + 10.0f); } completion:^(BOOL finished){ 
[UIView animateWithDuration:0.25f animations:^(){ 
pullImageView.center = CGPointMake(pullImageView.center.x,
 pullImageView.center.y - 10.0f); } completion:^(BOOL finished) { [self startMotionEffects]; }]; }]; 
 //延迟四秒后再执行。
 [self performSelector:@selector(wiggle) withObject:nil 
afterDelay:4.0f];}
- (void)startMotionEffects {
 UIInterpolatingMotionEffect * motionEffectX = [[UIInterpolatingMotionEffect alloc] 
initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; 
UIInterpolatingMotionEffect * motionEffectY =[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" 
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; 
motionEffectX.minimumRelativeValue = @-15.0; 
motionEffectX.maximumRelativeValue = @15.0; 
motionEffectY.minimumRelativeValue = @-15.0; 
motionEffectY.maximumRelativeValue = @15.0; 
motionEffectsGroup = [[UIMotionEffectGroup alloc] init]; 
motionEffectsGroup.motionEffects = @[motionEffectX, 
motionEffectY]; 
[pullImageView addMotionEffect:motionEffectsGroup];}
//移除加速度效果/
/[pullImageView removeMotionEffect:motionEffectsGroup];// motionEffectsGroup = nil;
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}
上一篇 下一篇

猜你喜欢

热点阅读