亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創建iPhone的擺動圖標效果?

如何創建iPhone的擺動圖標效果?

MMMHUHU 2019-10-05 10:57:36
我想在應用程序中來回擺動圖像,就像按下時在iPhone圖標上擺動一樣。最好的方法是什么?這是我首次嘗試不使用動畫GIF的動畫。我認為該方法是稍微前后旋轉圖像以產生擺動效果。我看過使用CABasicAnimation和CAKeyframeAnimation。CABasicAnimation每次重復都會產生一個抖動,因為它跳到起始位置并且不會向內插。CAKeyframeAnimation似乎是解決方案,除了我無法使其正常工作。我肯定錯過了什么。這是我使用CAKeyframeAnimation的代碼(不起作用):    NSString *keypath = @"wobbleImage";CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keypath];animation.duration = 1.0f;animation.delegate = self;animation.repeatCount = 5;CGFloat wobbleAngle = 0.0872664626f;NSValue *initial = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 0.0f, 1.0f)];NSValue *middle = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];NSValue *final = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];animation.values = [NSArray arrayWithObjects:initial, middle, final, nil];[imageView.layer addAnimation:animation forKey:keypath];或可能有一個我不曾想到的完全簡單的解決方案。感謝任何指針。謝謝!
查看完整描述

3 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

簡單的方法:


#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)


CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5.0));

CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));


itemView.transform = leftWobble;  // starting point


[UIView beginAnimations:@"wobble" context:itemView];

[UIView setAnimationRepeatAutoreverses:YES]; // important

[UIView setAnimationRepeatCount:10];

[UIView setAnimationDuration:0.25];

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];


itemView.transform = rightWobble; // end here & auto-reverse


[UIView commitAnimations];


...


- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 

{

     if ([finished boolValue]) {

        UIView* item = (UIView *)context;

        item.transform = CGAffineTransformIdentity;

     }

}

可能必須考慮時間和角度,但這應該可以幫助您入門。


編輯:我編輯了響應以添加代碼,以在完成后將項目恢復為原始狀態。另外,請注意,您可以使用beginAnimations上下文值將任何內容傳遞給start / stop方法。在這種情況下,它是擺動對象本身,因此您不必依賴特定的ivars,并且該方法可用于任何基于UIView的通用對象(即文本標簽,圖像等)。


查看完整回答
反對 回復 2019-10-05
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

Ramin的回答非常好,但是由于OS4使用一個簡單函數animateWithDuration也可以實現相同的效果。


(使他的榜樣適合未來的Google員工)


#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)


- (void)startWobble {

 itemView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5));


 [UIView animateWithDuration:0.25 

      delay:0.0 

      options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)

      animations:^ {

       itemView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5));

      }

      completion:NULL

 ];

}


- (void)stopWobble {

 [UIView animateWithDuration:0.25

      delay:0.0 

      options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear)

      animations:^ {

       itemView.transform = CGAffineTransformIdentity;

      }

      completion:NULL

  ];

}


查看完整回答
反對 回復 2019-10-05
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

您應該CAKeyframeAnimation用來制作更平滑的動畫。


+ (void) animationKeyFramed: (CALayer *) layer 

                   delegate: (id) object

              forKey: (NSString *) key {


    CAKeyframeAnimation *animation;

    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    animation.duration = 0.4;

    animation.cumulative = YES;

    animation.repeatCount = 2;

    animation.values = [NSArray arrayWithObjects:

            [NSNumber numberWithFloat: 0.0], 

            [NSNumber numberWithFloat: RADIANS(-9.0)], 

            [NSNumber numberWithFloat: 0.0],

            [NSNumber numberWithFloat: RADIANS(9.0)],

            [NSNumber numberWithFloat: 0.0], nil];

    animation.fillMode = kCAFillModeForwards;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    animation.removedOnCompletion = NO;

    animation.delegate = object;


    [layer addAnimation:animation forKey:key];

}


查看完整回答
反對 回復 2019-10-05
  • 3 回答
  • 0 關注
  • 562 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號