这是一个相当普遍的问题,对此我有几个答案,而且我快到了。我有一个按钮,按下时会创建一个图像(代码如下)
(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];
}
它的工作原理是,当我点击它们时,我会得到每个图像的标签输出。但是当我拖动时,它会闪烁……有什么想法吗?