app 摇一摇功能

1.重写canBecomeFirstResponder ,并返回YES

-(BOOL)canBecomeFirstResponder{
    return YES;
}

2.重写UIResponder的三个方法

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"在摇了");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    if (event.subtype == UIEventSubtypeMotionShake) {
        NSLog(@"stop");
        
        int i = arc4random_uniform(12);
        NSLog(@"%d",i);
        NSString *str = nil;
        if (i == 3) {
            str = @"你运气真好,中奖了";
        }else{
            str = @"你还有3次机会";
        }
        
        UIAlertView *yaoyao = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [yaoyao show];
    }
    
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"取消");
}
原文地址:https://www.cnblogs.com/gulong/p/4716116.html