网页浏览中的单个或多个水平滑块会造成麻烦。用户滑动滑块的拇指而不会导致页面视图滑动。
我尝试过的解决方法
答案 0 :(得分:5)
到目前为止,最好的结果是在自定义扩展的UISlider类中覆盖了Pan Gesture。会发生什么是在UISlider矩形中的任何地方检测到Pan Gesture而不仅仅是拇指。现在坚如磐石,没有浏览页面。
注意:提供了initWithFrame和initWithCoder,因此sictSlider2可以通过代码或通过XIB实例化。
如果用户点击滑块矩形内部,也会处理点按。 潘也有
[self sendActionsForControlEvents:UIControlEventValueChanged];
因为ValueChanged事件仅在移动拇指时发生,并且当Pan处理程序接管时拇指在稍微移动后停止移动。
Happy CutAndPaste!
代码:
#import "sictSlider2.h"
@implementation sictSlider2
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
UITapGestureRecognizer * recognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
recognizer2.delegate = self;
[self addGestureRecognizer:recognizer2];
UIPinchGestureRecognizer * recognizer3 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
recognizer3.delegate = self;
[self addGestureRecognizer:recognizer3];
UISwipeGestureRecognizer * recognizer4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
recognizer4.delegate = self;
[self addGestureRecognizer:recognizer4];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
UITapGestureRecognizer * recognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
recognizer2.delegate = self;
[self addGestureRecognizer:recognizer2];
UIPinchGestureRecognizer * recognizer3 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
recognizer3.delegate = self;
[self addGestureRecognizer:recognizer3];
UISwipeGestureRecognizer * recognizer4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
recognizer4.delegate = self;
[self addGestureRecognizer:recognizer4];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer{
CGPoint p = [recognizer locationInView:recognizer.view];
NSLog (@"in slider tap %f %f ", p.x, p.y);
float val = self.value; // current slider value
// t.x is in coords of the slider rect, so divide by width to get delta
val = p.x/self.frame.size.width;
[self setValue:val];
[self setNeedsDisplay];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint t = [recognizer translationInView:recognizer.view];
NSLog(@"in slider pan %f %f", t.x, t.y);
float val = self.value; // current slider value
// t.x is in coords of the slider rect, so divide by width to get delta
val = val + t.x/self.frame.size.width;
[self sendActionsForControlEvents:UIControlEventValueChanged];
[self setValue:val];
[self setNeedsDisplay];
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer{
NSLog(@"slider SWIPE");}
@end
和.h
#import <UIKit/UIKit.h>
@interface sictSlider2 : UISlider
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
- (void)handlePan:(UIPanGestureRecognizer *)recognizer;
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;
- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer;
@end