gpt4 book ai didi

ios - TouchesMoved 与 UITouch

转载 作者:行者123 更新时间:2023-11-29 03:26:58 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的应用程序,您可以通过触摸 UIImageView 并拖动他来移动 UIImageView。

我的 UIImageView 叫做 imv

-(void ) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch * touch = [touches anyObject];

if([touch view] == self.imv){
CGPoint location = [touch locationInView:self.view];
self.imv.center = location;

}
}

-(void ) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];

if([touch view] == self.imv){
CGPoint location = [touch locationInView:self.view];
self.imv.center = location;

}
}

我一整天都在尝试解决这个问题,但我不知道出了什么问题。如果我禁用 if 语句,它就可以工作,否则就不行。我能做什么?

感谢您的回答

最佳答案

除非您将 UIImageView 子类化(不太可能),否则您的 View 将接收触摸事件。

如今,使用 UIGestureRecognizer 来处理这种事情更简单也更常见,在本例中是 UIPanGestureRecognizer

例如

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragImageView:)];
[self.imv addGestureRecognizer:pan];


- (void)dragImageView:(UIPanGestureRecognizer *)dragImageView {
if(UIGestureRecognizerStateBegan == state) {
originalCenter = self.imv.center; // add CGPoint originalCenter; member
} else if(UIGestureRecognizerStateChanged == state) {
CGPoint translate = [pan translationInView:self.imv.superview];
self.imv.center = CGPointMake(originalCenter.x + translate.x, originalCenter.y + translate.y);
}
}

关于ios - TouchesMoved 与 UITouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337964/

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