一.模仿苹果删除app时的抖动效果
// 仿苹果删除app时抖动效果
#define angelToRandian(x) ((x)/180.0*M_PI)
CAKeyframeAnimation* anim=[CAKeyframeAnimation animation];
anim.keyPath=@"transform.rotation";
anim.values=@[@(angelToRandian(-4)),@(angelToRandian(4)),@(angelToRandian(-4))];
anim.repeatCount=MAXFLOAT;
anim.duration=0.3;
[view.layer addAnimation:anim forKey:nil];
// 恢复抖动
view.layer.speed = 1.0;
// 停止抖动
view.layer.speed = 0.0;
二.根据颜色绘制图片
+ (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContext(size);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
// Draw your image
[image drawInRect:rect];
// Get the image, here setting the UIImageView image
image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
三.视图左右震动动画
- (void)shakeAnimationForView:(UIView *) view {
CALayer *viewLayer = view.layer;
CGPoint position = viewLayer.position;
CGPoint x = CGPointMake(position.x + 1, position.y);
CGPoint y = CGPointMake(position.x - 1, position.y);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setFromValue:[NSValue valueWithCGPoint:x]];
[animation setToValue:[NSValue valueWithCGPoint:y]];
[animation setAutoreverses:YES];
[animation setDuration:.06];
[animation setRepeatCount:3];
[viewLayer addAnimation:animation forKey:nil];
}
四.避免多个按钮同时响应点击
设置view的exclusiveTouch属性为 YES(默认为NO); exclusiveTouch的意思是UIView会独占整个Touch事件,
具体的来说,就是当设置了exclusiveTouch的 UIView是事件的第一响应者,那么到你的所有手指离开前,
其他的视图UIview是不会响应任何触摸事件的,对于多点触摸事件,这个属性就非常重要,
值得注意的是:手势识别(GestureRecognizers)会忽略此属性。
五.UIView覆盖statusBar
在封装 UIAlertView或者UIActionSheet的时候需要黑色的遮罩层覆盖整个页面. 此时需要新建一个window并指定 window的level为 statusBar
UIWindow *win = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
指定window的level
win.windowLevel = UIWindowLevelStatusBar;
win.backgroundColor = [UIColor clearColor];
[win addSubview:self];
self.bgWindow = win;
[win makeKeyAndVisible];
[UIView animateWithDuration:kAnimateDuration animations:^{
self.backgroundView.alpha = 1.0f;
self.actionSheetView.frame =
CGRectMake(0, self.frame.size.height-self.actionSheetView.frame.size.height,
self.frame.size.width, self.actionSheetView.frame.size.height);
}];