2

好吧,这个想法是在带有字符的小圆圈上实现拖放功能。看起来很简单,但我无法立即完成。问题是代表小圆圈的 UIImageView 根本不响应平移。这是我写的代码:

- (void)createLiterasAndPlacesForThemForCard:(UIView *)frontCard andWord:(NSString *)word;
for (int i=0; i<[word length]; i++) {
    UIImageView * oneChar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LetterFull"]];
    oneChar.frame = CGRectMake(26+i*24, 175, 23, 24);
    oneChar.backgroundColor = [UIColor clearColor];
    oneChar.userInteractionEnabled = YES;
    UIPanGestureRecognizer * oneCharPanGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
    [oneCharPanGestureRecognizer setMinimumNumberOfTouches:1];
    [oneCharPanGestureRecognizer setMaximumNumberOfTouches:1];
    [oneCharPanGestureRecognizer setDelegate:self];
    [oneChar addGestureRecognizer:oneCharPanGestureRecognizer];
    [frontCard addSubview:oneChar];
}

这是层次结构 - 我有一个上面称为“self”的 viewController,它包含一个滚动视图,包含一些卡片,每张卡片都有可以拖动的小圆圈。我在设置委托的行收到警告,但我想这不是实际问题。

这是“move:”方法的代码:

- (void)move:(id)sender
{
    NSLog(@"THE PAN OCCURED");
    float firstX;
    float firstY;

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        firstX = [[sender view] center].x;
        firstY = [[sender view] center].y;
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [[sender view] setCenter:translatedPoint];
}

而且我看不到对平底锅的任何反应——无论是在控制台还是在屏幕上。请帮助我,我自己找不到解决方案,我已经启用了 userInteraction 并没有帮助。

4

0 回答 0