iOS 图标抖动算法 [英] iOS icon jiggle algorithm

查看:16
本文介绍了iOS 图标抖动算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 iPad 应用程序,它呈现用户文档的方式类似于 Pages 呈现它们的方式(作为实际文档的大图标).我还想模仿用户点击编辑按钮时的抖动行为.这与您在 iPhone 和 iPad 上点击并按住图标时在主屏幕上产生的抖动模式相同.

I am writing an iPad app that presents user documents similar to the way Pages presents them (as large icons of the actual document). I also want to mimic the jiggling behavior when the user taps the edit button. This is the same jiggle pattern that icons make on the home screen when you tap and hold on them on both the iPhone and iPad.

我在互联网上搜索并找到了一些算法,但它们只会导致视图来回晃动,这与 Apple 的抖动完全不同.似乎有一些随机性,因为每个图标的摇晃都略有不同.

I've searched the Internet and have found a few algorithms but they just cause the view to rock back and forth which is not at all like the Apple jiggle. It seems there is some randomness in there as each icon jiggles a little differently.

有没有人有或知道一些可以重新创建相同抖动模式(或非常接近它的东西)的代码?谢谢!!!

Does anyone have or know of some code that can re-create the same jiggle pattern (or something very close to it)? Thanks!!!

推荐答案

好的,所以 openspringboard 代码对我来说并没有完全做到,但我确实允许我创建一些我认为更好的代码,但仍然没有完美但更好.如果有人有更好的建议,我很想听听他们...(将此添加到您想要摇晃的视图的子类中)

OK, so the openspringboard code didn't quite do it for me but I did allow me to create some code that I think is a bit better, still not prefect but better. If anyone has suggestions to make this better, I would love to hear them... (add this to the subclass of the view(s) you want to jiggle)

#define degreesToRadians(x) (M_PI * (x) / 180.0)
#define kAnimationRotateDeg 1.0
#define kAnimationTranslateX 2.0
#define kAnimationTranslateY 2.0

- (void)startJiggling:(NSInteger)count {

    CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) ));
    CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) ));
    CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY);
    CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);

    self.transform = leftWobble;  // starting point

    [UIView animateWithDuration:0.1
                          delay:(count * 0.08)
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{ self.transform = conCatTransform; }
                     completion:nil];
}

- (void)stopJiggling {
    [self.layer removeAllAnimations];
    self.transform = CGAffineTransformIdentity;  // Set it straight 
}

这篇关于iOS 图标抖动算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆