区分两个 ImageView

发布于 2024-12-08 19:12:45 字数 245 浏览 0 评论 0原文

我有两个图像视图 imageView1 和 imageView2。我已经给了gestureRecognizer来移动这两个图像。现在的问题是,当我将第一个 imageView 移动到第二个 imageView 附近时,仅显示第二个图像视图。但是当我将第一个 imageView 放在另一个图像上时,第一个 imageView 应该显示在第二个 imageView 上,但没有发生。任何人都可以告诉我第一个 imageView 如何在另一个 imageView 的顶部获得视图。

I have two imageViews imageView1 and imageView2. I have given gestureRecognizer to move these both images. Now the problem is when i move first imageView near second imageView only second image view is displayed. But when i put first imageView on another image the first imageView should be displayed on the second imageView which is not happening.Can any body please tell me how can the first imageView gets view on the top of another imageView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-12-15 19:12:45

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

You can use the UIView method bringSubviewToFront:, and pass in the UIImageView that you want displayed on top.

陌上青苔 2024-12-15 19:12:45

你为什么不使用类似的东西来拖动你的 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

Why dont you use something similar like this to drag your 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;
    }

}

//---EDIT---//
Added: cliptobounds in viewdidload

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文