iOS开发中实用代码片段

一.模仿苹果删除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);
        }];

 

转载于:https://my.oschina.net/zhxx/blog/715843

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值