当我有这行代码时
UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)];
and this
- (void)dragGestureChanged:(UILongPressGestureRecognizer*)gesture{
...
}
我想在“@selector(dragGestureChanged:)”处添加一个参数“(UIScrollView*)scrollView”,我该怎么办?
你不能直接——UIGestureRecognizer
我们知道如何向仅接受一个参数的选择器发出调用。为了完全通用,您可能希望能够传递一个块。 Apple 尚未内置该功能,但添加起来相当容易,至少如果您愿意对手势识别器进行子类化,则可以解决添加新属性并在其后正确清理的问题,而无需深入研究运行时。
所以,例如(随我所写,未经检查)
typedef void (^ recogniserBlock)(UIGestureRecognizer *recogniser);
@interface UILongPressGestureRecognizerWithBlock : UILongPressGestureRecognizer
@property (nonatomic, copy) recogniserBlock block;
- (id)initWithBlock:(recogniserBlock)block;
@end
@implementation UILongPressGestureRecognizerWithBlock
@synthesize block;
- (id)initWithBlock:(recogniserBlock)aBlock
{
self = [super initWithTarget:self action:@selector(dispatchBlock:)];
if(self)
{
self.block = aBlock;
}
return self;
}
- (void)dispatchBlock:(UIGestureRecognizer *)recogniser
{
block(recogniser);
}
- (void)dealloc
{
self.block = nil;
[super dealloc];
}
@end
然后你可以这样做:
UILongPressGestureRecognizer = [[UILongPressGestureRecognizerWithBlock alloc]
initWithBlock:^(UIGestureRecognizer *recogniser)
{
[someObject relevantSelectorWithRecogniser:recogniser
scrollView:relevantScrollView];
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)