Ios 我可以将手势识别器的代码放在视图的子类中吗?
我的程序中有一个主视图,其中有一个可拖动的视图。可以使用平移手势拖动此视图。目前,虽然它使用了大量代码,但我希望将这些代码放入子类中以降低复杂性。(我最终希望通过允许用户通过进一步的平移手势扩展视图来增加功能。这意味着如果我不能首先解决这个问题,将会有更多的代码阻塞我的视图控制器) 是否可以在类的子类中包含手势识别器的代码,并且仍然与父类中的视图交互 这是我用于在父类中启用平移手势的当前代码:Ios 我可以将手势识别器的代码放在视图的子类中吗?,ios,objective-c,inheritance,subclass,uipangesturerecognizer,Ios,Objective C,Inheritance,Subclass,Uipangesturerecognizer,我的程序中有一个主视图,其中有一个可拖动的视图。可以使用平移手势拖动此视图。目前,虽然它使用了大量代码,但我希望将这些代码放入子类中以降低复杂性。(我最终希望通过允许用户通过进一步的平移手势扩展视图来增加功能。这意味着如果我不能首先解决这个问题,将会有更多的代码阻塞我的视图控制器) 是否可以在类的子类中包含手势识别器的代码,并且仍然与父类中的视图交互 这是我用于在父类中启用平移手势的当前代码: -(void)viewDidLoad { ... UIView * draggab
-(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];
}
我现在已经添加了视图的子类,但无法确定如何或在何处创建手势识别器,因为视图现在正在子类中创建并添加到父类中
我真的希望手势识别器的目标在这个子类中,但是当我尝试编码它时,什么都没有发生
我曾尝试将所有代码放在子类中,并将平移手势添加到视图中,但当我尝试拖动它时,访问失败
我目前正在尝试使用
[graphView addSubview:[[BDraggableView alloc] getDraggableView]];
将其添加到子视图,然后在子类的函数GetDragableView中设置视图(添加平移手势等)
必须有一种更直接的方法来实现这一点,我还没有概念化——我对处理子类还是相当陌生,所以我还在学习如何将它们组合在一起
感谢您提供的任何帮助我认为您希望对
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.
}
我想我可能会想出这个办法 在父类中,我创建了子类变量:
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];
}
我花了一段时间才弄明白,我们希望在子类中进行所有设置,然后将此视图及其特性添加到父视图中
感谢所有的答案,前提是他们让我按照正确的思路来解决这个问题