iphone::touchesMoved:将 UIImageView 移动到 UIView 内?

发布于 2024-11-24 07:54:54 字数 1326 浏览 1 评论 0原文

这是我的代码:

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

CGPoint pt = [[touches anyObject] locationInView:self.view];
startLocation = pt;
[super touchesBegan: touches withEvent: event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
CGRect frame = [self.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self.view setFrame:frame];
}


- (void)viewDidLoad
{
[super viewDidLoad];

UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];


UIImageView *imv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boo2.png"]];
imv.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:imv];

[self.view addSubview: v];
[v release];

UIView *v2 = [[UIView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)];
v2.backgroundColor = [UIColor blueColor];


UIImageView *imv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boo2.png"]];
imv2.frame = CGRectMake(0, 0, 50, 50);
[v2 addSubview:imv2];

[self.view addSubview: v2];
[v2 release];
}

我想要做的是将 UIImageView 移动到 UIView 内,而不是整个视图。但是,上面的代码,当我尝试拖动时,它会移动整个视图。

注意:图像是从其他函数返回的,未在 viewdidload 中声明。假设您无法声明 UIImageView 变量。

This is my code:

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

CGPoint pt = [[touches anyObject] locationInView:self.view];
startLocation = pt;
[super touchesBegan: touches withEvent: event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
CGRect frame = [self.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self.view setFrame:frame];
}


- (void)viewDidLoad
{
[super viewDidLoad];

UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];


UIImageView *imv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boo2.png"]];
imv.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:imv];

[self.view addSubview: v];
[v release];

UIView *v2 = [[UIView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)];
v2.backgroundColor = [UIColor blueColor];


UIImageView *imv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boo2.png"]];
imv2.frame = CGRectMake(0, 0, 50, 50);
[v2 addSubview:imv2];

[self.view addSubview: v2];
[v2 release];
}

What I want to do is to move the UIImageView inside the UIView, not the entire view. But, the code above, when I try to drag, it moves the entire view.

NOTE: The images are return from others functions, not declared in viewdidload. Assume, you cant declare UIImageView variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-01 07:54:54

我不太确定你想做什么。我不知道为什么您将每个 UIImageView's 放入您放入视图控制器视图中的 UIView 中。您也可以直接将 UIImageView's 添加为视图控制器视图的子视图。

尽管如此,据我所知,用户似乎必须能够拖动屏幕上的图像。

如果是这种情况,我建议您将每个 UIImageView 存储在一个数组中。然后,在 touchesBegan:withEvent: 中,您必须确定哪个 UIImageView 正在被触摸。正如您在代码中所做的那样,获取代表主视图中位置的 CGPoint 是一个好的开始。如果您直接将 UIImageView's 添加为主视图的子视图,则可以将此 CGPoint 的坐标与这些 UIImageView's 的框架进行比较> 确定哪一个被按下。

然后,为了允许拖动,您需要在触摸开始时保存 UIImageView 的坐标。然后,每次触摸移动时,您都可以将 UIImageView 的新 x 位置计算为原始 x 位置,加上移动的距离。也就是说,

newImageViewX = imageViewStartX + (newTouchX - touchStartX);

y 位置也类似。

I'm not quite sure what you're trying to do. and I don't know why you put each of the UIImageView's inside a UIView which you put into the view controller's view. You can just as well add the UIImageView's as sub-views of the view controller's view directly.

Nevertheless, from what I can make out it looks like the user must be able to drag the images on the screen around.

If this is the case, I would suggest that you store each one of the UIImageView's in an array. Then, in touchesBegan:withEvent:, you will have to determine which UIImageView is being touched. Getting the CGPoint representing the location in the main view, as you do in your code, is a good start. If you added the UIImageView's directly as sub-views of the main view, you can then compare the coordinates of this CGPoint with the frames of these UIImageView's to determine which one is being pressed.

Then, to allow dragging, you need to save the coordinates of the UIImageView at the beginning of the touch. Then, everytime the touch moves, you can compute the new x-position of the UIImageView as the original x-position, plus the distance moved. That is,

newImageViewX = imageViewStartX + (newTouchX - touchStartX);

and similarly for the y-position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文