iphone::touchesMoved:将 UIImageView 移动到 UIView 内?
这是我的代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定你想做什么。我不知道为什么您将每个
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 aUIView
which you put into the view controller's view. You can just as well add theUIImageView'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, intouchesBegan:withEvent:
, you will have to determine whichUIImageView
is being touched. Getting theCGPoint
representing the location in the main view, as you do in your code, is a good start. If you added theUIImageView's
directly as sub-views of the main view, you can then compare the coordinates of thisCGPoint
with the frames of theseUIImageView'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 theUIImageView
as the original x-position, plus the distance moved. That is,newImageViewX = imageViewStartX + (newTouchX - touchStartX);
and similarly for the y-position.