由於各種原因,我已將這些方法從UIView子類移到我的viewcontroller。我終於搞定了,除了一件事。我不僅可以拖動以編程方式創建的UIImageviews,但實際的視圖控制器視圖也可以拖動。造成這種不良影響。我猜這是觸及anyobject的事實,而背景本身就是一個對象。我只是不確定如何排除背景。我會認爲它需要「UserInteraction啓用」,但我猜不是?我只希望它使UIImageView可拖動。請原諒我的noobness。我還在學習。touchesbegan,touchesmoved,touchesended問題
我有一個名爲「letterDictionary」的NSMutableDictionary中的所有圖像我想要「可觸摸」。是否有可能只適用於字典中的內容?
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
touchPoint = [touch locationInView:self.view];
movingLetter = [touch view];
CGPoint pointInside = [touch locationInView:[touch view]];
if ([movingLetter pointInside:pointInside withEvent:event]) touchedInside = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view]; // get the new touch location
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
touchPoint = newPoint;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view];
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
if (CGRectIntersectsRect([movingLetter frame], [placeHolder frame]))
{
movingLetter.center = placeHolder.center;
}
}
touchedInside = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
touchedInside = NO;
}
,完美的工作!我應該知道這樣做!出於好奇,但是,你或者有人告訴我如何將它添加到我的NSMutableDictionary中的對象(UIImageViews)? – Jason
請參閱http://stackoverflow.com/questions/4073598/user-interaction-on-a-uiimageview – cdasher