1

大家好,

我第一次使用“触摸方法”所以面临某些问题。在我的应用程序中,mainview 中有 2 个子视图。我正在尝试将 uibutton 从一个视图拖放到另一个视图。我正在尝试很多事情,但无法进行拖放。

请帮我。

提前致谢。

My Code :


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];
        for (UIButton *iView in self.view.subviews) {
            if ([iView isMemberOfClass:[UIButton class]])
            {
                if (touchPoint.x > iView.frame.origin.x &&
                    touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
                    touchPoint.y > iView.frame.origin.y &&
                    touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
                {
                    self.btnalpha = iView;
                    touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
                                                   touchPoint.y - iView.frame.origin.y);

                    homePosition = CGPointMake(iView.frame.origin.x,
                                                    iView.frame.origin.y);

                    [self.viewblue bringSubviewToFront:self.btnalpha];
                }
            }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
                                           touchPoint.y - touchOffset.y,
                                           self.btnalpha.frame.size.width,
                                           self.btnalpha.frame.size.height);
    self.btnalpha.frame = newDragObjectFrame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self.viewblue];

    if (touchPoint.x > self.vieworange.frame.origin.x &&
        touchPoint.x < self.vieworange.frame.origin.x + self.vieworange.frame.size.width &&
        touchPoint.y > self.vieworange.frame.origin.y &&
        touchPoint.y < self.vieworange.frame.origin.y + self.vieworange.frame.size.height )
    self.btnalpha.frame = CGRectMake(self.homePosition.x, self.homePosition.y, self.btnalpha.frame.size.width, self.btnalpha.frame.size.height);
}

- (void) drag
{
    str_scramble =@"IH";

    for (int i = 0; i < 2; i++)
    {
        self.btnalpha = [[UIButton alloc] init];
        [self.btnalpha setFrame:CGRectMake(i * 50 + 10, 5, 42, 42)];
        char letter = [str_scramble characterAtIndex:i];
        self.btnalpha.backgroundColor = [UIColor grayColor];
        self.btnalpha.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [self.btnalpha setTitle:[NSString stringWithFormat:@"%c", letter] forState:UIControlStateNormal];
        [self.btnalpha addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
        [self.btnalpha addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [viewblue addSubview:self.btnalpha];
        NSLog(@"string : --> %@", self.btnalpha.titleLabel.text);
    }
}
4

3 回答 3

0

您应该使用 touchDrag 方法,如 touchDragEnter、touchDragExit、touchDragInside 或 touchDragOutside 来拖动 UIButton。

这些所有事件都是 UIControlEvents

于 2013-03-04T09:20:46.100 回答
0

根据关于事件处理指南的苹果文档,
当你第一次触摸按钮时,它会变成第一响应者。所以,你的 ViewController 中的 touchesBegan:withEvent 不会被调用。

禁用 UIbutton 的 userInteraction,现在 viewcontroller 将成为第一响应者并开始接收触摸事件。
button.userInteractionEnabled=否;

即使您将手势添加到 ViewController 或任何这些视图,当您触摸按钮时,它们也不会响应。

于 2013-12-18T11:33:49.423 回答
0

我正在使用 PanGestureRecognizer,效果很好。

UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragButton:)];
            drag.delegate = self;
            drag.view.tag = position;
            [button addGestureRecognizer:drag];

- (void)dragButton:(UIPanGestureRecognizer *)gesture
{
    buttonPicked = [allObjects objectAtIndex:gesture.view.tag -1];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalPoint = buttonPicked.center;
    }
    else
    {
        buttonPicked.center = [gesture locationInView:self.view];
    }
}
  • 如果您想对结束手势进行某种特殊处理(例如仅在某个位置下降) - 您可以使用 UIGestureRecognizedStateEnd。

  • 如果它仍然不起作用 - 在 UIGestureRecognizerStateBegan 中,尝试从子视图中删除按钮并将其添加到主视图中,并在 UIGestureRecognizerStateEnd 检查按钮在哪个子视图中并添加它而不是主视图。应该做的伎俩。

祝你好运。

于 2013-09-01T15:51:08.677 回答