一、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];
}
代码下载地址
二、补充
-
edgesForExtendedLayout
在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],
它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。
这就是为什么所有的UI元素都往上漂移了44pt。
可以下载上面的demo尝试在AppDelgate
里面修改edgesForExtendedLayout
的值,可以看到明显的效果。
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;
-
CGRectInset
一般用于
1.在Touch
事件中扩大按钮点击响应范围
CGRectInset(view1.frame, -20, -20);
2.用于下面的。自定义UITextField中控制 placeHolder 的位置,以及控制文本的位置。
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}
// text position
- (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}
网友评论