【问题标题】:Differentiate between two ImageViews区分两个 ImageView
【发布时间】:2011-10-07 12:07:12
【问题描述】:

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

【问题讨论】:

    标签: objective-c cocoa-touch ipad uiimageview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它工作得非常好,但是当它靠近第二个 imageView 时它没有剪裁。
      • 您是否设置了 clipToBounds (BOOL) 属性?
      • 是的,我在 touches move 方法中将其设置为 yes。但我仍然遇到同样的问题。
      • 在 viewdidload 中执行并发布结果
      • 我已将此属性设置为 viewDidLoad 中的两个 imageViews,但我遇到了同样的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多