0

可能重复:
我如何在 IBAction(按钮)内放置一个 void(摇动手势)?

您好,我正在创建一个应用程序,我需要帮助。我之前已经问过这个问题 2 次,但我没有得到正确的答案,要么是讲解工作,要么是这个人没有意识到我是一个完全的初学者。所以在回答之前请解释一下答案,因为我对 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 是用户按下按钮“播放”时的动作

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

4

2 回答 2

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
  }
}
于 2012-06-30T12:13:48.497 回答
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 = @"";
}

添加动作开始

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake && canStartAnimation) {
        [self doTheAnimation];
    }
}
于 2012-06-30T12:15:39.347 回答