2012-02-25 66 views
3

我需要我的登錄屏幕如下所示。Facebook喜歡登錄屏幕和驗證(Window Shake Effect)

Facebooks登錄時,如果將用戶名或密碼字段留空並單擊登錄按鈕,user namepassword字段會振動(如抖動)。

這是如何完成的?

enter image description here

+1

您認爲這樣做是怎麼回事?設置一些定時器將字段左移一些像素,然後移回右側。 – 0xDE4E15B 2012-02-25 19:42:38

+2

我需要知道,如果它的單元格或標籤使用。 – shajem 2012-02-26 18:49:01

+1

對我來說就像一個UITableView。分組,2行,也許用文本字段作爲他們的意見。 – 2012-02-27 03:18:50

回答

5

可以使用CABasicAnimation實現這一目標。看看這個UIView shake animation

CABasicAnimation *animation = 
         [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setDuration:0.05]; 
[animation setRepeatCount:8]; 
[animation setAutoreverses:YES]; 
[animation setFromValue:[NSValue valueWithCGPoint: 
       CGPointMake([lockView center].x - 20.0f, [lockView center].y)]]; 
[animation setToValue:[NSValue valueWithCGPoint: 
       CGPointMake([lockView center].x + 20.0f, [lockView center].y)]]; 
[[lockView layer] addAnimation:animation forKey:@"position"]; 

可可申請看看Core Animation Tutorial: Window Shake Effect

- (CAKeyframeAnimation *)shakeAnimation:(NSRect)frame 
{ 
int numberOfShakes = 4; 
float durationOfShake = 0.2f; 
float vigourOfShake = 0.04f; 
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animation]; 

    CGMutablePathRef shakePath = CGPathCreateMutable(); 
    CGPathMoveToPoint(shakePath, NULL, NSMinX(frame), NSMinY(frame)); 
    int index; 
    for (index = 0; index < numberOfShakes; ++index) 
    { 
     CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame) - frame.size.width * vigourOfShake, NSMinY(frame)); 
     CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame) + frame.size.width * vigourOfShake, NSMinY(frame)); 
    } 
    CGPathCloseSubpath(shakePath); 
    shakeAnimation.path = shakePath; 
    shakeAnimation.duration = durationOfShake; 
    return shakeAnimation; 
} 

    [window setAnimations:[NSDictionary dictionaryWithObject:[self shakeAnimation:[window frame]] forKey:@"frameOrigin"]]; 
    [[window animator] setFrameOrigin:[window frame].origin]; 
+0

這是正確的方法.... – Kamarshad 2012-02-25 20:25:23