2011-10-07 59 views
1

我有两个imageViews imageView1和imageView2。我已经给了gestureRecognizer来移动这两个图像。现在的问题是,当我第一次移动imageView靠近第二个图像时,只显示第二个图像视图。但是当我把第一个imageView放在另一个图像上时,第一个imageView应该显示在第二个imageView上,这是不会发生的。可以请任何人告诉我第一个imageView如何在另一个imageView的顶部获取视图。区分两个ImageViews

回答

1

为什么不使用类似的东西来拖动你的UIImageView?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.view.backgroundColor = [UIColor yellowColor]; 

    test1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    test1.backgroundColor = [UIColor whiteColor]; 
    test1.userInteractionEnabled = YES; 
    test1.clipToBounds = YES; 
    [self.view addSubview:test1]; 

    test2 = [[UIImageView alloc] initWithFrame:CGRectMake(400, 400, 100, 100)]; 
    test2.backgroundColor = [UIColor whiteColor]; 
    test2.userInteractionEnabled = YES; 
    test2.clipToBounds = YES; 
    [self.view addSubview:test2]; 
} 


CGPoint startLocation; 
float diffX; 
float diffY; 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == test1) 
    { 
     startLocation = [touch locationInView:self.view]; 
     [self.view bringSubviewToFront:test1]; 
    } 

    if([touch view] == test2) 
    { 
     startLocation = [touch locationInView:self.view]; 
     [self.view bringSubviewToFront:test2]; 
    } 
} 


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

    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == test1) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     diffX = location.x - startLocation.x; 
     diffY = location.y - startLocation.y; 
     test1.frame = CGRectMake(test1.frame.origin.x + diffX, test1.frame.origin.y + diffY, test1.frame.size.width, test1.frame.size.height); 
     startLocation = test1.frame.origin; 
    } 

    if([touch view] == test2) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     diffX = location.x - startLocation.x; 
     diffY = location.y - startLocation.y; 
     test2.frame = CGRectMake(test2.frame.origin.x + diffX, test2.frame.origin.y + diffY, test2.frame.size.width, test2.frame.size.height); 
     startLocation = test2.frame.origin; 
    } 

} 

// --- 编辑 --- // 补充:在viewDidLoad中cliptobounds

+0

据工作绝对没问题,但是当涉及第二ImageView的附近是没有限。 –

+0

是否设置了clipToBounds(BOOL)属性? –

+0

是的,我已经把它设置为是在触动移动method.But仍然我有同样的问题。 –

2

您可以使用UIView方法bringSubviewToFront:,并通过UIImageView您想显示在​​顶部。