如何檢測Xcode中的圖像是否被觸摸?我已經看過蘋果的文檔,它真的是彆扭...... 我看到:如何檢測圖像是否被觸摸
if (CGRectContainsPoint([imageView frame], location))
,但我的形象仍不會移動。我嘗試使用的touchesBegan + touchesMoved,並設置圖像的userInteractionIsEnabled爲YES,但它仍然不會檢測到它:(
編輯:謝謝大家這麼多的偉大的建議最終,我真的想讓它儘可能簡單,我知道我的代碼應該工作,所以我一直用它擺弄,和一個良好的夜間睡眠後,我意識到這是一個相當簡單的解決方案:
在我的動作移動:
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGRect shapeRect = [imageView frame];
CGRect dropSquareRect = [dropSquare frame];
CGPoint touchLocation = CGPointMake(location.x, location.y);
if (CGRectContainsPoint(shapeRect, touchLocation))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[imageView setCenter:touchLocation];
[UIView commitAnimations];
}
if (CGRectIntersectsRect(shapeRect, dropSquareRect))
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.imageView.alpha = 0;
self.imageView.center = CGPointMake(dropSquare.center.x, dropSquare.center.y);
self.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
[UIView commitAnimations];
這是應該做的......上帝的答案 –