2010-01-02 89 views
8

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

它的工作原理是,当我点击它们时,我得到了每个图像的标签输出。但是当我拖动时,它闪烁......任何想法?

回答

34

回答我自己的问题 - 我决定创建一个类来处理我放置在视图上的图像。

代码,如果任何人的兴趣....

Draggable.h

#import <Foundation/Foundation.h> 
@interface Draggable : UIImageView { 
    CGPoint startLocation; 
} 
@end 

Draggable.m

#import "Draggable.h" 
@implementation Draggable 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    startLocation = pt; 
    [[self superview] bringSubviewToFront:self]; 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Move relative to the original touch point 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    CGRect frame = [self frame]; 
    frame.origin.x += pt.x - startLocation.x; 
    frame.origin.y += pt.y - startLocation.y; 
    [self setFrame:frame]; 
} 
@end 

,并把它称为

UIImage *tmpImage = [[UIImage imageNamed:"test.png"] retain]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:tmpImage]; 

CGRect cellRectangle; 
cellRectangle = CGRectMake(0,0,tmpImage.size.width ,tmpImage.size.height); 
UIImageView *dragger = [[Draggable alloc] initWithFrame:cellRectangle]; 
[dragger setImage:tmpImage]; 
[dragger setUserInteractionEnabled:YES]; 

[self.view addSubview:dragger]; 
[imageView release]; 
[tmpImage release]; 
+4

上面的代码来自http://www.iphoneexamples.com/ – 2010-01-02 19:43:20

+0

感谢张贴这个,但我得到抖动的效果,而这样做,它可以如何避免? – itsaboutcode 2012-04-26 00:43:02

+0

谢谢你!像魅力一样工作:) – soupdiver 2013-08-28 10:17:58

1

当您更改center时,通常会显示隐式动画。你有没有搞乱-contentMode或打电话-setNeedsDisplay

您可以明确要求的动画,以避免删除和重新绘制这样:

if (touch.view.tag > 0) { 
    [UIView beginAnimations:@"viewMove" context:touch.view]; 
    touch.view.center = location; 
    [UIView commitAnimations]; 
} 

请注意NSLog()可能会很慢(远远低于你所期望的,它比一个更复杂简单的printf),并且这可能会导致经常出现的问题,如touchesMoved:withEvent:

顺便说一句,你漏水tmpImage

+0

感谢您的答复。我试过你的建议,但它仍然发生。我认为这与我用来设置图像的代码有关?当我在Interface Builder中的视图上放置图像并将其设置为.h文件中的IBOutlet UIImageView时,该图像完美地移动。我在设置每张图像时是否错过了一些东西? (ps)感谢tmpImage提示 - 错过了那一个! – 2010-01-02 17:15:10

+0

我认为这可能是一个问题与标签..但现在我认为这可能是一个问题与意见。当我使用第一位代码放置图像时,它会变为0,0。当我拖动它时,它似乎总想回到那里。所以这是导致闪烁。当前的位置,然后它回到那里......它驱使我疯狂! – 2010-01-02 17:50:47