UITouch,拖放根本不起作用

时间:2012-08-09 11:31:17

标签: iphone objective-c ios xcode uitouch

我有一个UIView,在他的顶部我有UIButton,UITableView和UINavigationBar。我想要做的是,当你点击UIButton并拖动它时,所有视图将只移动到左侧,直到屏幕结束。我的意思是你可以将视图拖到左边,当你停止触摸时,视图将停在手指停止触摸屏幕的位置。

我不能这样做,他只给我看了“触动开始1”,就是这样。这似乎是我的问题?

非常感谢大家!

float oldX, oldY;
BOOL dragging;

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog("Touches Began 1");
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(btnOpen.frame, touchLocation))
    {
        NSLog("Touches Began 2");
        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (dragging)
    {
        NSLog("Dragging!");
        CGRect frame = mainView.frame;
        frame.origin.x = mainView.frame.origin.x + touchLocation.x - oldX;
        frame.origin.y =  mainView.frame.origin.y + touchLocation.y - oldY;
        mainView.frame = frame;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog("Touches Ended!");
    dragging = NO;
}


- (void)viewDidLoad
{    
    [btnOpen addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
    [btnOpen addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents: UIControlEventTouchDragInside];
    [btnOpen addTarget:self action:@selector(touchesEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}

2 个答案:

答案 0 :(得分:2)

您要求触摸相对于主视图框架的坐标

CGPoint touchLocation = [touch locationInView:self.view];

然后你问touchLocation的位置是否在按钮框架的框架内

if (CGRectContainsPoint(btnOpen.frame, touchLocation))

换句话说。您正在检查触摸是否在按钮的范围内,但您可以通过主视图上的触摸坐标来检查,而不是按钮。

所以这就是你应该做的:

CGPoint touchLocation = [touch locationInView:btnOpen];

答案 1 :(得分:2)

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    NSLog("Touches Began 1");
for (UITouch *touch in touches) {
    //UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(btnOpen.frame, touchLocation))
    {
        NSLog("Touches Began 2");
        //dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
   // UITouch *touch = [[event allTouches] anyObject];
for (UITouch *touch in touches) {
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(btnOpen.frame, touchLocation))
    {
        NSLog("Dragging!");
        CGRect frame = mainView.frame;
        frame.origin.x = mainView.frame.origin.x + touchLocation.x - oldX;
        frame.origin.y =  mainView.frame.origin.y + touchLocation.y - oldY;
        mainView.frame = frame;
    }
}
}

尝试这样我觉得它会对你有所帮助。