简体   繁体   English

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

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

I have a main view in my program with a draggable view in it. 我的程序中有一个主视图,其中有一个可拖动的视图。 This view can be dragged around with pan gestures. 可以使用平移手势拖动此视图。 Currently though it uses a lot of code which I want to put in a subclass to reduce complexity. 目前,尽管它使用了大量代码,但我想将它们放在子类中以降低复杂性。 (I eventually want to increase functionality by allowing the user to expand with view with further pan gestures. This means there will be lots more code clogging up my view controller if I can't sort this out first) (我最终希望通过允许用户使用进一步的平移手势扩展视图来增强功能。这意味着如果我不能首先解决这个问题,将会有更多的代码阻塞我的视图控制器)

Is it possible to have the code for a gesture recogniser in the subclass of a class and still interact with views in the parent class. 是否可以在一个类的子类中有用于手势识别器的代码,并且仍然与父类中的视图交互。

This is the current code I am using to enable pan gesture in the parent class: 这是我用来在父类中启用平移手势的当前代码:

-(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];
}

(Thanks to Rob Mayoff for getting me this far) (感谢Rob Mayoff让我走了这么远)

I have now added a subclass of the view but can't figure out how or where I need to create the gesture recogniser as the view is now being created in the subclass and added to the parent class. 现在,我已经添加了视图的子类,但是由于现在正在子类中创建视图并将其添加到父类中,因此无法弄清创建手势识别器的方式或位置。

I really want the target for the gesture recogniser to be in this subclass but when I try to code it nothing happens. 我确实希望手势识别器的目标位于此子类中,但是当我尝试编码时,什么也没有发生。

I have tried putting all the code in the subclass and adding the pan gesture to the view but then I get a bad access crash when I try to drag it. 我尝试将所有代码放在子类中,然后将平移手势添加到视图中,但是当我尝试拖动它时,访问崩溃。

I am currently trying to use 我目前正在尝试使用

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

To add it to the subview and then setting up the view (adding the pan gesture etc) in the function getDraggableView in the subclass 要将其添加到子视图中,然后在子类的功能getDraggableView中设置视图(添加平移手势等)

There must be a more straight forward way of doing this that I haven't conceptualised yet - I am still pretty new dealing with subclasses and so am still learning how they all fit together. 我必须尚未概念化一种更直接的方法-我还是处理子类的新手,因此仍在学习它们如何组合在一起。

Thanks for any help you can give 谢谢你提供的所有帮助

I think I might of figured this one out. 我想我可能想到了这一点。

In the parent class I created the child class variable: 在父类中,我创建了子类变量:

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

This allowed me to initialise the child class with the view I wanted to have the draggable view on: graphView 这使我可以使用希望在其上具有可拖动视图的视图来初始化子类:graphView

Then in the child view I set up the pan gesture as I normally would but added it to this view carried through: 然后在子视图中,像往常一样设置平移手势,但通过以下操作将其添加到该视图中:

- (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];
}

It took me a while to straighten it in my head that we want to do all the setting up in our subclass and then add this view with its characteristics to the parent view. 我花了好一会儿才想起要在子类中进行所有设置,然后将此视图及其特征添加到父视图中。

Thanks for all the answers provided they got me thinking along the right lines to solve it 感谢您提供的所有答案,他们让我沿着正确的方向思考以解决问题

I think you want to subclass UIView and make your own DraggableView class. 我认为您想UIView并创建自己的DraggableView类。 Here, you can add swipe and pan gesture recognizers. 在这里,您可以添加滑动和平移手势识别器。 This would be in the implementation of a subclass of UIView 这将在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