[英]How to called TouchesMoved for 2 different UIImageView
您好,我在UIView
有2個UIImageViews
。
一旦我觸摸UIImageView
touchesBegan
就會調用touchesBegan
方法。 但是,一旦我在UIImageView
拖動,就會調用touchesMoved
。 但同時第二個UIImageView
touchesMoved
也被調用。
您能幫我兩個UIImageViews
如何獲取touchesMoved
事件嗎?
這是我的代碼
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
NSLog(@"iv1 Begin");
if (CGRectContainsPoint(iv2.frame,currentPoint)==YES)
NSLog(@"iv2 Begin");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if(CGRectContainsPoint(iv1.frame,currentPoint)==YES)
NSLog(@"iv1 Moved NO");
if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
NSLog(@"iv1 Moved YES");
if(CGRectContainsPoint(iv2.frame,currentPoint)==YES)
NSLog(@"iv2 Moved NO");
if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
NSLog(@"iv2 Moved NO");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
NSLog(@"iv1 End");
if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
NSLog(@"iv2 End");
}
您可以使用鏈接到兩個視圖的兩個插座,這是用於在觸摸方法中重新識別兩個視圖的代碼:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
touchedView = t.view;
if (t.view == view1) {
//todo something with view1;
} else if (t.view == view2) {
//todo something with view2
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.