私は、パスアニメーションを使用して画面を丸く丸めた円画像を持っています。そして私はユーザーが動いているサークルに触れるときを検出したい。しかし、画像が連続した円で動いているにもかかわらず、そのフレームは左上隅にまだ移動していません。これを更新して動画のタッチを検出するにはどうすればいいですか?ここでは、コード...のviewDidLoadでUIImageViewアニメーションのタッチを検出
セットアップアニメーションは、次のとおりです。
//set up animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 10.0;
pathAnimation.repeatCount = 1000;
CGMutablePathRef curvedPath = CGPathCreateMutable();
//path as a circle
CGRect bounds = CGRectMake(60,170,200,200);
CGPathAddEllipseInRect(curvedPath, NULL, bounds);
//tell animation to use this path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
//add subview
circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
[testView addSubview:circleView];
//animate
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
触れる方法:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//detect touch
UITouch *theTouch = [touches anyObject];
//locate and assign touch location
CGPoint startPoint = [theTouch locationInView:self.view];
CGFloat x = startPoint.x;
CGFloat y = startPoint.y;
//create touch point
CGPoint touchPoint = CGPointMake(x, y);
//check to see if the touch is in the rect
if (CGRectContainsPoint(circleView.bounds, touchPoint)) {
NSLog(@"yes");
}
//check image view position
NSLog(@"frame x - %f, y - %f", circleView.frame.origin.x, circleView.frame.origin.y);
NSLog(@"center x - %f, y - %f", circleView.center.x, circleView.center.y);
NSLog(@"bounds x - %f, y - %f", circleView.bounds.origin.x, circleView.bounds.origin.y);
}
ImageViewのは、ちょうど左上隅に滞在しているようです。私は、タッチのジェスチャーが動いているボール上に作られたかどうかをどのように認識するかを理解するように思えます。
任意の助けをいただければ幸い、
クリス
最も簡単な方法の1つは、カスタムのタイプのUIButtonを作成し、画像をボタンの背景として追加することです。その後、あなたはそれが助けてくれることを願っています:) – Luke
こんにちは、私はまずそれを試してみました。しかし、それは私が何をしているのか全く分からなかった。私は「touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event」を使用して、ユーザーが画像を保持できる時間を確認したいからです。確かにイメージの位置にアクセスする方法がなければなりませんか?乾杯、 – ChrisM