区分两个ImageView

时间:2011-10-07 12:07:13

标签: objective-c cocoa-touch ipad uiimageview

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

2 个答案:

答案 0 :(得分:2)

您可以使用UIView方法bringSubviewToFront:,然后传入您想要显示在顶部的UIImageView

答案 1 :(得分:1)

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

- (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