仅在某些选定的视图标签上允许触摸事件

时间:2009-12-17 11:51:34

标签: iphone cocoa-touch

我是iPhone开发的新手,做我的第一个应用程序。

我在屏幕上有一些标签

在这些标签中,我必须在几个选定的

上设置拖动事件

拖动代码是

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

现在我希望拖动只实现几个选定的标签。

我通过继承UILabel

来实现这一点
@interface DragView : UILabel
{
    CGPoint startLocation;
}
@end

@implementation DragView
- (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

但我希望在没有子类化

的情况下实现这一点

有没有办法做到这一点

1 个答案:

答案 0 :(得分:4)

您可以将UILabel的{​​{3}}属性设置为NO