[英]how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?
i wonder if you guys have a sample code adding and move UIImageView on touch and also detect if there is an UIImageView there so i can't add UIImageView on top of it. 我想知道你们是否有一个示例代码添加和移动UIImageView,并检测是否有一个UIImageView,所以我不能在它上面添加UIImageView。
EDIT: clearer question 1. i wanna add an cups(UIImageView) when i touch the view but do not wan the cup(UIImageView) to stack. 编辑:更清楚的问题1.我想在触摸视图时添加一个杯子(UIImageView),但不要将杯子(UIImageView)堆叠起来。
thanks for reading my question and appreciated your helps 感谢您阅读我的问题并感谢您的帮助
cheers 干杯
des DES
You can add a UIPanGestureRecognizer to the UIImageView to handle the dragging, as seen below. 您可以将UIPanGestureRecognizer添加到UIImageView来处理拖动,如下所示。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageView addGestureRecognizer:panRecognizer];
Implement the following method: 实现以下方法:
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
}
In combination to the above, you can use CGRectIntersectsRect to find if it's intersecting another cup. 结合上述内容,您可以使用CGRectIntersectsRect查找它是否与另一个杯子相交。
BOOL isIntersecting = CGRectIntersectsRect ([cup1 frame],[cup2 frame]);
You would have to wrap the functionality to take into account all your cups but this gives you an idea a to how to accomplish what you're trying to do. 您必须将功能包装起来考虑所有的杯子,但这可以让您了解如何完成您想要做的事情。
This should let you going… 这应该让你去......
Create two ivars in your header file: 在头文件中创建两个ivars:
UIImageView *myImageView1;
UIImageView *myImageView2;
The next two methods will be in your implementation file: 接下来的两个方法将在您的实现文件中:
-(void)initImagesAndPanGesture
{
UIImage *img = [UIImage imageNamed:@"image1.png"];
myImageView1 = [[UIImageView alloc]initWithImage:img];
[myImageView1 setFrame:CGRectMake(300, 800, 100, 100)];
[myImageView1 setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
[myImageView1 addGestureRecognizer:recognizer1];
[self.view addSubview:myImageView1];
img = [UIImage imageNamed:@"image2.png"];
myImageView2 = [[UIImageView alloc]initWithImage:img];
[myImageView2 setFrame:CGRectMake(500, 800, 100, 100)];
[myImageView2 setUserInteractionEnabled:YES];
recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
[myImageView2 addGestureRecognizer:recognizer1];
[self.view addSubview:myImageView2];
}
-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
if (!(CGRectIntersectsRect(myImageView1.frame, myImageView2.frame)))
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
else
{
NSLog(@"intersect!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.