OStack程序员社区-中国程序员成长平台

标题: ios - 平移 uiview 在触摸方向移动,iphone [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:58
标题: ios - 平移 uiview 在触摸方向移动,iphone

我想将 UIView 平移到与触摸移动相同的方向。

请提出建议。



Best Answer-推荐答案


修改 touchesBegan 和 touchesMoved 方法如下

float oldX, oldY;
BOOL dragging;

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(window.frame, touchLocation)) {

        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}

- (void)touchesMovedNSSet *)touches withEventUIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (dragging) {

        CGRect frame = window.frame;
        frame.origin.x = window.frame.origin.x + touchLocation.x - oldX;
        frame.origin.y =  window.frame.origin.y + touchLocation.y - oldY;
        window.frame = frame;
    }

    oldX = touchLocation.x;
    oldY = touchLocation.y;
}

- (void)touchesEndedNSSet *)touches withEventUIEvent *)event {

    dragging = NO;
}

希望对你有帮助

关于ios - 平移 uiview 在触摸方向移动,iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254426/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4