2

我试图创建一个应该可以拖动的标签。但是拖动的事件没有触发(用断点检查)。下面是代码。

 - (IBAction)InsertText:(UIBarButtonItem *)sender {

    UILabel *lblWatermark = [[UILabel alloc] initWithFrame:currentImage.frame];
    lblWatermark.text = @"Copyright";
    lblWatermark.userInteractionEnabled = YES;
    [lblWatermark sizeToFit];


    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(labelDragged:)];
    [lblWatermark addGestureRecognizer:gesture];
    [currentImage addSubview:lblWatermark];
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
    UILabel *label = (UILabel *)gesture.view;
    CGPoint translation = [gesture translationInView:label];

    // move label
    label.center = CGPointMake(label.center.x + translation.x,
                               label.center.y + translation.y);
        [gesture setTranslation:CGPointZero inView:label];
}

先感谢您,

4

2 回答 2

5

发现了错误。其实我也得做

userInteractionEnabled = 是

不仅适用于标签,而且适用于我在其中添加标签的 Imageview。

于 2012-09-24T18:51:34.993 回答
0

确保您的 ViewController 是带有(.h 文件)的 UIGestureRecognizer 的委托:

@interface View : UIView <UIGestureRecognizerDelegate>

然后设置:

 gesture.delegate = self;
于 2012-09-24T18:11:31.680 回答