【问题标题】:UIImage detecting touch and draggingUIImage 检测触摸和拖动
【发布时间】:2010-01-02 15:28:25
【问题描述】:

这是一个相当常见的问题,我有几个答案,而且我快到了。我有一个按钮,按下时会创建一个图像(代码如下)

(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];
}

它的工作原理是,当我单击它们时,我会为每个图像获得标签的输出。但是当我拖动时,它会闪烁……有什么想法吗?

【问题讨论】:

    标签: iphone uiimageview touch


    【解决方案1】:

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

    如果有人感兴趣,请编码....

    可拖动的.h

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

    可拖动的.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];
    

    【讨论】:

    • 感谢您发布此内容,但我在执行此操作时会出现抖动效果,如何避免?
    【解决方案2】:

    当您更改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

    【讨论】:

    • 感谢您的回复。我已经尝试了你的建议,它仍然会发生。我认为这与我用来设置图像的代码有关吗?当我在 Interface Builder 中的视图上放置图像并将其设置为 .h 文件中的 IBOutlet UIImageView 时,它会完美移动。设置每个图像时我是否遗漏了什么? (ps) 感谢 tmpImage 提示 - 错过了!
    • 我认为这可能是标签的问题.. 但现在我认为这可能是视图的问题。当我使用第一位代码放置图像时,它变为 0,0。当我拖动它时,它似乎总是想回到那里。所以这导致了闪烁。当前位置,然后它又移回那里……这让我发疯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多