2011-10-07 61 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您想顯示在​​頂部。