我试着在动摇iPhone之后打电话给谷歌页面。我试过这样做:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
[self shakemethod];
[self open];
}
}
-(void)shakemethod
{
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
[shake setDuration:1.1];
[shake setRepeatCount:2];
[shake setAutoreverses:YES];
[shake setFromValue:[NSValue valueWithCGPoint:
CGPointMake(lockImage.center.x - 15,lockImage.center.y)]];
[shake setToValue:[NSValue valueWithCGPoint:
CGPointMake(lockImage.center.x + 15, lockImage.center.y)]];
[lockImage.layer addAnimation:shake forKey:@"position"];
}
-(void)open
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}
两种方法都奏效了,但当我摇晃iPhone时,抖动的图像没有显示,而是谷歌页面打开了。我需要当我摇动手机第一摇图片,抖完后,摇动打开谷歌网页。
发布于 2014-10-07 04:09:54
将自己设置为抖动动画,然后在动画结束时调用open方法。
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
[self shakemethod];
}
}
-(void)shakemethod
{
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
[shake setDuration:1.1];
[shake setRepeatCount:2];
[shake setAutoreverses:YES];
//set animation delgate to self
[shake setDelegate:self];
[shake setFromValue:[NSValue valueWithCGPoint:
CGPointMake(lockImage.center.x - 15,lockImage.center.y)]];
[shake setToValue:[NSValue valueWithCGPoint:
CGPointMake(lockImage.center.x + 15, lockImage.center.y)]];
[lockImage.layer addAnimation:shake forKey:@"position"];
}
//when animation will finish call the open method
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self open];
}
-(void)open
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.co.in"]];
}
希望这是你想要的。
https://stackoverflow.com/questions/26235538
复制相似问题