摇一摇功能的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/potato512/article/details/83014403

实现摇一摇功能须在视图控制器中进行编码。
首先将当前视图控制器设置为第一响应者;其次实现摇一摇的三个协议方法。
示例如下:

[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
[self becomeFirstResponder];
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
        [self.message appendString:@"开始摇一摇功能\n"];
        
        self.label.text = self.message;
    }
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self.message appendString:@"结束摇一摇功能\n"];
    
    self.label.text = self.message;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //摇动结束
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        [self.message appendString:@"取消摇一摇功能\n"];
        
        self.label.text = self.message;
        // 振动效果 需要#import <AudioToolbox/AudioToolbox.h>
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}

猜你喜欢

转载自blog.csdn.net/potato512/article/details/83014403