gpt4 book ai didi

iphone - UITouch,拖放根本不起作用

转载 作者:行者123 更新时间:2023-11-28 19:16:18 24 4
gpt4 key购买 nike

我有一个 UIView,在他上面我有 UIButton、UITableView 和 UINavigationBar。我想要做的是,当您单击 UIButton 并拖动它时,所有 View 将仅移动到左侧,直到屏幕结束。我的意思是,您可以向左拖动 View ,当您停止触摸时, View 将停止在您手指停止触摸屏幕的位置。

我做不到,他只给我看了“Touches Began 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];
}

最佳答案

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

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

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

if (CGRectContainsPoint(btnOpen.frame, touchLocation))

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

所以这是你应该做的:

CGPoint touchLocation = [touch locationInView:btnOpen];

关于iphone - UITouch,拖放根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11882443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com