相当普遍的问题,我有几个答案,我几乎在那里。我有一个按钮按下时,将(如下代码)创建图像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