用户按下按钮后如何激活动作抖动?

时间:2012-06-30 12:04:42

标签: iphone ios xcode button motion

  

可能重复:
  How do i put a void(shake gesture) inside a IBAction(button)?

你好我正在创建一个应用程序,我需要帮助。我之前已经问了两次这个问题,但是我没有得到正确的答案,无论是讲解工作还是那个人都没有意识到我是一个初学者。所以在回答之前请解释一下答案,因为我对Xcode很新。所以我有一个按钮说播放,当前点击它时动画会顺利显示然后结束。我需要改变的是当用户按下播放时,一个标签说摇动设备开始。然后当用户摇动设备时标签消失并且动画播放和结束,这就是我目前在viewcontroller.m文件中所拥有的:

-(IBAction)startanimation {





animation.animationImages= [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"Frame0.png"],
                                [UIImage imageNamed:@"Frame1.png"],
                                [UIImage imageNamed:@"Frame2.png"],
                                [UIImage imageNamed:@"Frame3.png"],
                                [UIImage imageNamed:@"Frame4.png"],
                                [UIImage imageNamed:@"Frame5.png"],
                                [UIImage imageNamed:@"Frame6.png"],
                                [UIImage imageNamed:@"Frame7.png"],
                                [UIImage imageNamed:@"Frame8.png"],
                                [UIImage imageNamed:@"Frame9.png"],
                                [UIImage imageNamed:@"Frame10.png"],
                                [UIImage imageNamed:@"Frame11.png"],
                                [UIImage imageNamed:@"Frame12.png"],
                                [UIImage imageNamed:@"Frame13.png"],
                                [UIImage imageNamed:@"Frame14.png"], 
                                nil];

    [animation setAnimationRepeatCount:1];
    animation.animationDuration = 1;
    [animation startAnimating];



}

顺便说一句,IBAction:startanimation是当用户按下“播放”按钮时的动作

请考虑我是编码的初学者,如果您有任何问题可以随意提问,谢谢!

2 个答案:

答案 0 :(得分:2)

方法

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

您只需使用此代码:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (event.type == UIEventSubtypeMotionShake) {
    //Your code here
  }
}

答案 1 :(得分:1)

在标题文件(.h)中添加以下内容

@interface YourViewController : UIViewController
{
    IBOutlet UILabel *label;
    BOOL canStartAnimation;
}

也改变了开始动画

-(IBAction)startanimation 
{
    canStartAnimation = YES;
    label.text = @"shake to animate";
}

添加此功能

-(void) doTheAnimation {
    animation.animationImages= [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"Frame0.png"],
                                [UIImage imageNamed:@"Frame1.png"],
                                [UIImage imageNamed:@"Frame2.png"],
                                [UIImage imageNamed:@"Frame3.png"],
                                [UIImage imageNamed:@"Frame4.png"],
                                [UIImage imageNamed:@"Frame5.png"],
                                [UIImage imageNamed:@"Frame6.png"],
                                [UIImage imageNamed:@"Frame7.png"],
                                [UIImage imageNamed:@"Frame8.png"],
                                [UIImage imageNamed:@"Frame9.png"],
                                [UIImage imageNamed:@"Frame10.png"],
                                [UIImage imageNamed:@"Frame11.png"],
                                [UIImage imageNamed:@"Frame12.png"],
                                [UIImage imageNamed:@"Frame13.png"],
                                [UIImage imageNamed:@"Frame14.png"], 
                                nil];

    [animation setAnimationRepeatCount:1];
    animation.animationDuration = 1;
    [animation startAnimating];   
    label.text = @"";
}

添加motionBegan

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake && canStartAnimation) {
        [self doTheAnimation];
    }
}