【问题标题】:Pass name to Subclass of UIView将名称传递给 UIView 的子类
【发布时间】:2011-04-08 05:41:18
【问题描述】:

我创建了 UIView 的子类来处理 hitTest 和 Touches 操作,因为 UIView 是 UIScrollview 的子视图,我需要 UIView 和 UIScrollView 都是可拖动的。

我已经让它工作了 95% - 如果我设置背景颜色,子类 UIView 可以完美运行 - 但是如果我设置一个带有透明图像的子视图,它并不总是响应。我对此有一些想法,所以这不是问题。

我的问题是,由于我在 UIScrollview 上有多个子类 UIView 的实例,我怎么知道哪一个被拖动了。

在以前的应用程序中,我只是简单地使用过;

if ([touch view] == blah)   {           
//do something;  
}  
else if ([touch view] == fred) {  
//do nothing;  
}  

但是因为它位于我的主应用程序 .m 文件中,所以没关系,因为 blah 和 fred 都是在 viewdidload 中创建的,但是由于我的代码中的触摸事件是在子类中捕获的,所以他们不知道 blah 和 fred .

所以 1) 我如何让子类知道我添加的项目 - 或 -
2) 我如何将项目的名称传递给 hitTest 或 TouchesBegan 中的子类 - 或 -
3)有没有一行代码可以获取当前项的名称

非常感谢任何帮助。

【问题讨论】:

    标签: iphone object uiview uiscrollview subclass


    【解决方案1】:

    好的,这就是您可以使用 NotificationCenter 轻松完成的方法。

    需要知道拖动哪个子视图的对象可以监听名为“SubviewIsDragged”的通知,每个子视图都可以发送通知“SubviewIsDragged”作为参数传递。

    这是代码:

    对象在它的init方法中需要知道拖拽寄存器如下:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subviewIsDragged) name:@"SubviewIsDragged" object:nil];
    

    发现拖拽后执行动作的方法:

    -(void)subviewIsDragged:(NSNotification *)notification
    {
        //Here you can get your subclass of UIView
        UIView* view = [notification object];
    
        // do what ever you want with the object
    }
    

    现在在您的 UIView 子类中将以下内容添加到您的拖动方法中:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"SubviewIsDragged" object:self];
    

    请注意,您传递了对象“self”,但您可以传递您想要的任何其他对象,例如 NSString ID。

    希望得到帮助。

    【讨论】:

      【解决方案2】:

      将 panGesture 识别器添加到您的子视图类

      UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
          [panGesture setMaximumNumberOfTouches:2];
          [panGesture setDelegate:self];
          [self addGestureRecognizer:panGesture];
      

      使用此代码进行翻译,拖动错误视图不会有任何问题

      - (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
      {
      
      [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
      
      if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
      
          CGPoint translation = [gestureRecognizer translationInView:[self superview]];
      
          self.center = CGPointMake([self center].x + translation.x, [self center].y + translation.y);
          [gestureRecognizer setTranslation:CGPointZero inView:[self superview]];
      }
      }
      

      【讨论】:

        【解决方案3】:

        所以对于其他也有同样问题的人。

        我找到了一个简单的解决方案:

        在我的 viewDidLoad 中,当我添加子类子视图时,我只是在行中添加
        customView1.tag = 111;

        然后在我的触摸方法中的子类中我可以使用:
        if ([touch view].tag == 111) {

        简单! :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-20
          • 2021-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多