gpt4 book ai didi

ios - iPhone - 沿触摸和拖动路径移动对象

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

我正在开发一个简单的动画,其中 UIImageView 沿着 UIBezierPath 移动,现在我想为移动的 UIImageView 提供用户交互,所以该用户可以通过触摸 UIImageView 并在屏幕上拖动 UIImageview 来引导 UIImageView

最佳答案

更改touchesMoved:withEvent:中 ImageView 位置的框架.

编辑:一些代码

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isEqual: self.view] || touch.view == nil) {
return;
}

lastLocation = [touch locationInView: self.view];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch.view isEqual: self.view]) {
return;
}

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

CGFloat xDisplacement = location.x - lastLocation.x;
CGFloat yDisplacement = location.y - lastLocation.y;

CGRect frame = touch.view.frame;
frame.origin.x += xDisplacement;
frame.origin.y += yDisplacement;
touch.view.frame = frame;
lastLocation=location;
}

您还应该实现 touchesEnded:withEvent:touchesCanceled:withEvent:

关于ios - iPhone - 沿触摸和拖动路径移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10327541/

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