这是一个相当常见的问题,我有一些答案,但还差一点。我有一个按钮,按下它时会创建一张图片(代码如下)。
(numImages 在加载时设置为零,并用作创建的所有图像标记号的计数器)
(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 的方法,它将检测被触摸的内容。我需要它允许用户拖动新创建的图像。现在基本能够工作,但是当你拖动它时,图像会在屏幕上闪烁。我可以通过获取TAG来访问所点击的图像,但我无法使其平稳地被拖动。
- (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];
}
它可以工作,当我点击每个图像时,我会得到每个图像的标签输出。但是当我拖动时,它会闪烁...有什么想法吗?