我有一个简单的应用程序,包含2个视图。顶视图有一个加速计代表。当用户在屏幕上显示顶视图时摇晃。然后调用pushviewcontroller并出现子视图。问题是当子视图出现时,我摇动它,它仍然捕获摇动动作并导致我错误。所以帮助我。提前谢谢。
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float violence = 1.2;
static BOOL beenhere;
BOOL shake = FALSE;
if (beenhere) return;
beenhere = TRUE;
if (acceleration.x > violence || acceleration.x < (-1* violence))
shake = TRUE;
if (acceleration.y > violence || acceleration.y < (-1* violence))
shake = TRUE;
if (acceleration.z > violence || acceleration.z < (-1* violence))
shake = TRUE;
if (shake) {
[self playSound:@"suzu"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"noVib"] == NO) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
}
当调用播放方法和视频完成时,将调用pushviewcontroller并显示子视图
下面的是一个处理所有电影播放器内容的类。
- (void) initPlayer{
if (mMoviePlayer != nil){
[mMoviePlayer release];
}
mMoviePlayer = [[MoviePlayerViewController alloc] initWithContentURL:[self createURL]];
[[NSNotificationCenter defaultCenter] removeObserver:mMoviePlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer.moviePlayer];
[mMoviePlayer.moviePlayer setShouldAutoplay:NO];
mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
mMoviePlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer.moviePlayer];
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer.moviePlayer];
[self dismissModalViewControllerAnimated:YES];
[mMoviePlayer release];
mMoviePlayer = nil;
[self toNext];
}
答案 0 :(得分:1)
我想到的第一件事就是跟踪是否按下了mMoviePlayer。
if (shake && mPlayerPushed) {
[self playSound:@"suzu"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"noVib"] == NO) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
mPlayerPushed = YES;
}
调用dismissModalViewControllerAnimated后,在moviePlayBackDidFinish方法中:是的,你应该将mPlayerPushed设置为NO。
或者您可以使用属性模态视图控制器来查看是否显示模态视图。我尚未测试但它应该可以工作。
modalViewController:活动模态视图的控制器 - 即 暂时显示在由...管理的视图顶部的视图 收件人。 (只读)
@property(nonatomic, readonly) UIViewController *modalViewController
所以代替bool mPlayerPushed,你将拥有:
if(shake && something.modalviewController == nil){ present mMoviePlayer }