摇一摇

 摇一摇

新建ShakeView继承自UIView  并实现下面4个方法

-(BOOL)canBecomeFirstResponder{

return YES;

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

if (event.type == UIEventSubtypeMotionShake) {

//Your code here

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 

message:@"你摇了一下哦!" 

  delegate:self 

  cancelButtonTitle:@"ok" 

  otherButtonTitles:nil];

[alert show];

[alert release];

}

}

 

 

然后在UIViewController中定义ShakeView   ShakeView载入后 调用[shake becomeFirstResponder];

你可能感兴趣的:(继承,方法,新建)