繁体   English   中英

我可以将手势识别器的代码放在视图的子类中吗?

[英]Can I put the code for a gesture recogniser in the subclass of a view?

我的程序中有一个主视图,其中有一个可拖动的视图。 可以使用平移手势拖动此视图。 目前,尽管它使用了大量代码,但我想将它们放在子类中以降低复杂性。 (我最终希望通过允许用户使用进一步的平移手势扩展视图来增强功能。这意味着如果我不能首先解决这个问题,将会有更多的代码阻塞我的视图控制器)

是否可以在一个类的子类中有用于手势识别器的代码,并且仍然与父类中的视图交互。

这是我用来在父类中启用平移手势的当前代码:

-(void)viewDidLoad {

    ...


   UIView * draggableView = [[UIView alloc]initWithFrame:CGRectMake(highlightedSectionXCoordinateStart, highlightedSectionYCoordinateStart, highlightedSectionWidth, highlightedSectionHeight)];
draggableView.backgroundColor = [UIColor colorWithRed:121.0/255.0 green:227.0/255.0 blue:16.0/255.0 alpha:0.5];
   draggableView.userInteractionEnabled = YES;

   [graphView addSubview:draggableView];

   UIPanGestureRecognizer * panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];

   [draggableView addGestureRecognizer:panner];

}

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

    UIView * draggedView = panner.view;
    CGPoint offset = [panner translationInView:draggedView.superview];
    CGPoint center = draggedView.center;

    // We want to make it so the square won't go past the axis on the left
    // If the centre plus the offset

    CGFloat xValue = center.x + offset.x;

    draggedView.center = CGPointMake(xValue, center.y);

    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:draggedView.superview];
}

(感谢Rob Mayoff让我走了这么远)

现在,我已经添加了视图的子类,但是由于现在正在子类中创建视图并将其添加到父类中,因此无法弄清创建手势识别器的方式或位置。

我确实希望手势识别器的目标位于此子类中,但是当我尝试编码时,什么也没有发生。

我尝试将所有代码放在子类中,然后将平移手势添加到视图中,但是当我尝试拖动它时,访问崩溃。

我目前正在尝试使用

[graphView addSubview:[[BDraggableView alloc] getDraggableView]];

要将其添加到子视图中,然后在子类的功能getDraggableView中设置视图(添加平移手势等)

我必须尚未概念化一种更直接的方法-我还是处理子类的新手,因此仍在学习它们如何组合在一起。

谢谢你提供的所有帮助

我想我可能想到了这一点。

在父类中,我创建了子类变量:

BDraggableView * draggableViewSubClass;
draggableViewSubClass = [[BDraggableView alloc] initWithView:graphView andRangeChart:   [rangeSelector getRangeChart]];

这使我可以使用希望在其上具有可拖动视图的视图来初始化子类:graphView

然后在子视图中,像往常一样设置平移手势,但通过以下操作将其添加到该视图中:

- (id)initWithView:(UIView *) view andRangeChart: (ShinobiChart *)chart {
    self = [super initWithNibName:nil   bundle:nil];
    if (self) {
        // Custom initialization
        parentView = view;

        [self setUpViewsAndPans];
    }
return self;
}

- (void)setUpViewsAndPans {

    draggableView = [[UIView alloc]initWithFrame:CGRectMake(highlightedSectionXCoordinateStart, highlightedSectionYCoordinateStart, highlightedSectionWidth, highlightedSectionHeight)];
    draggableView.backgroundColor = [UIColor colorWithRed:121.0/255.0 green:227.0/255.0 blue:16.0/255.0 alpha:0.5];
    draggableView.userInteractionEnabled = YES;

    // Add the newly made draggable view to our parent view
    [parentView addSubview:draggableView];
    [parentView bringSubviewToFront:draggableView];

    // Add the pan gesture
    UIPanGestureRecognizer * panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];
    [draggableView addGestureRecognizer:panner];
}

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

    UIView * draggedView = panner.view;
    CGPoint offset = [panner translationInView:draggedView.superview];
    CGPoint center = draggedView.center;

    CGFloat xValue = center.x + offset.x;

    draggedView.center = CGPointMake(xValue, center.y);

    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:draggedView.superview];
}

我花了好一会儿才想起要在子类中进行所有设置,然后将此视图及其特征添加到父视图中。

感谢您提供的所有答案,他们让我沿着正确的方向思考以解决问题

我认为您想UIView并创建自己的DraggableView类。 在这里,您可以添加滑动和平移手势识别器。 这将在UIView的子类的实现中

- (id)initWithFrame:(CGRect)frame
{
     if (self = [super initWithFrame:frame]) {
         UIGestureRecognizer *gestRec = [[UIGestureRecognizer alloc] initWithTarget:self 
                                                                             action:@selector(detectMyMotion:)];
         [self addGestureRecognizer:gestRec];
     }
     return self;
}

- (void)detectMyMotion:(UIGestureRecognizer *)gestRect
{
    NSLog(@"Gesture Recognized");
    // maybe even, if you wanted to alert your VC of a gesture...
    [self.delegate alertOfGesture:gestRect];
    // your VC would be alerted by delegation of this action.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM