相當普遍的問題,我有幾個答案,我幾乎在那裏。我有一個按鈕按下時,將(如下代碼)創建圖像UIImage檢測觸摸和拖動
(numImages上負載設置爲零,並且被用作計數開創建的所有圖像的標籤號)
UIImage *tmpImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", sender.tag]] retain];
UIImageView *myImage = [[UIImageView alloc] initWithImage:tmpImage];
numImages += 1;
myImage.userInteractionEnabled = YES;
myImage.tag = numImages;
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release];
然後我有一個touchesBegan方法,它會檢測到什麼被觸摸。我需要做的是讓用戶拖動新創建的圖像。它幾乎可以工作,但拖動它時,圖像會在整個地方閃爍。我可以訪問你點擊的圖像,因爲我可以得到它的標籤,但我不能很好地拖動它。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (touch.view.tag > 0) {
touch.view.center = location;
}
NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
}
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
它的工作原理是,當我點擊它們時,我得到了每個圖像的標籤輸出。但是當我拖動時,它閃爍......任何想法?
上面的代碼來自http://www.iphoneexamples.com/ – 2010-01-02 19:43:20
感謝張貼這個,但我得到抖動的效果,而這樣做,它可以如何避免? – itsaboutcode 2012-04-26 00:43:02
謝謝你!像魅力一樣工作:) – soupdiver 2013-08-28 10:17:58