美文网首页
iOS View 抖动效果

iOS View 抖动效果

作者: Ever_Blacks | 来源:发表于2016-08-15 10:45 被阅读180次

    在最近的项目中使用到View抖动的效果,就从StackOverflow搬运了一个在此记录一下。

    - (void)earthquake:(UIView*)itemView {
        CGFloat t = 2.0;
        CGAffineTransform leftQuake  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0);
        CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0); //水平晃动
        
        itemView.transform = leftQuake;  // starting point
        
        [UIView beginAnimations:@"earthquake" context:(__bridge void *)(itemView)];
        [UIView setAnimationRepeatAutoreverses:YES]; // 如果不加这一句 整个动画感觉不连贯
        [UIView setAnimationRepeatCount:5];
        [UIView setAnimationDuration:0.07];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];
        
        itemView.transform = rightQuake; // end here & auto-reverse
        
        [UIView commitAnimations];
    }
    
    - (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
        if ([finished boolValue]) {
            UIView* item = (__bridge UIView *)context;
            item.transform = CGAffineTransformIdentity;
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS View 抖动效果

          本文链接:https://www.haomeiwen.com/subject/omrusttx.html