iOS,自定义 UIView 上的 UISwipeGestureRecognizer - 不工作
iOS, UISwipeGestureRecognizer on Custom UIView - not working
我无法在我的自定义视图上设置成功的 UISwipeGestureRecognizer。
我已经成功地在主要 self.view
上进行了测试,但我无法让它在我的自定义上运行。
Parent.m
@property (strong, nonatomic) UISummaryChartView *chartView;
...
- (void)viewDidLoad {
[super viewDidLoad];
self.chartView = [[UISummaryChartView alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.chartView openBox];
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];
[swipeGest setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.view addGestureRecognizer:swipeGest];
}
-(void)swipeDetected: (UISwipeGestureRecognizer *) sender{
NSLog(@"swiped..");
if(self.chartView.frame.size.height > 50){
//chart view is open
NSLog(@"closing");
[self.chartView closeBox];
}
}
UISummaryChartView.h
UISummaryChartView : UIView
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super init];
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
CGFloat startx = 2;
CGFloat starty = 0;
self.width = self.viewWidth - 10;
self.height = (self.viewHeight / 100) * 50;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(0, 10, self.mainView.frame.size.width, 1);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 setBackgroundColor:[UIColor colorWithRed:(float)126/255.0 green:(float)35/255.0 blue:(float)32/255.0 alpha:1]];
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
}];
}
您需要在每个视图上使用单独的手势。
在您的自定义视图中添加另一个 UISwipeGestureRecognizer,方向设置为向下并调用 closebox 方法。
UISwipeGestureRecognizer *swipeGestDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeBox)];
[swipeGestDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[yourCustomView addGestureRecognizer:swipeGestDown];
要完成这项工作,您需要做两件事:
首先,您的 UIView
子类没有 frame
或 bounds
,因为您使用的是 init
而不是 initWithFrame:
其次,您必须在主 UIViewController
上设置一个 delegate
并实现 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
方法。这有效:
在UISummaryCharView.m
#import "UISummaryChartView.h"
@implementation UISummaryChartView
@synthesize mainView;
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super initWithFrame:theView.frame]; // THIS IS IMPORTANT
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
CGFloat startx = 2;
CGFloat starty = 0;
self.width = self.viewWidth - 10;
self.height = (self.viewHeight / 100) * 50;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
NSLog(@"frame %@",NSStringFromCGRect(self.frame));
NSLog(@"bounds %@",NSStringFromCGRect(self.bounds));
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(0, 10, self.mainView.frame.size.width, 1);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 setBackgroundColor:[UIColor colorWithRed:(float)126/255.0 green:(float)35/255.0 blue:(float)32/255.0 alpha:1]];
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
NSLog(@"frame %@",NSStringFromCGRect(self.frame));
NSLog(@"bounds %@",NSStringFromCGRect(self.bounds));
}];
}
-(void)closeBox{
NSLog(@"close box");
// your closeBox method
}
@end
在Parent.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.chartView = [[UISummaryChartView alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.chartView openBox];
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];
[swipeGest setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addSubview:self.chartView];
self.chartView.userInteractionEnabled = YES;
[self.chartView addGestureRecognizer:swipeGest];
swipeGest.delegate = self;
}
-(void)swipeDetected: (UISwipeGestureRecognizer *) sender{
NSLog(@"swiped..");
if(self.chartView.frame.size.height > 50){
//chart view is open
NSLog(@"closing");
[self.chartView closeBox];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(@"shouldReceiveTouch?");
return YES;
}
我无法在我的自定义视图上设置成功的 UISwipeGestureRecognizer。
我已经成功地在主要 self.view
上进行了测试,但我无法让它在我的自定义上运行。
Parent.m
@property (strong, nonatomic) UISummaryChartView *chartView;
...
- (void)viewDidLoad {
[super viewDidLoad];
self.chartView = [[UISummaryChartView alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.chartView openBox];
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];
[swipeGest setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self.view addGestureRecognizer:swipeGest];
}
-(void)swipeDetected: (UISwipeGestureRecognizer *) sender{
NSLog(@"swiped..");
if(self.chartView.frame.size.height > 50){
//chart view is open
NSLog(@"closing");
[self.chartView closeBox];
}
}
UISummaryChartView.h
UISummaryChartView : UIView
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super init];
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
CGFloat startx = 2;
CGFloat starty = 0;
self.width = self.viewWidth - 10;
self.height = (self.viewHeight / 100) * 50;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(0, 10, self.mainView.frame.size.width, 1);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 setBackgroundColor:[UIColor colorWithRed:(float)126/255.0 green:(float)35/255.0 blue:(float)32/255.0 alpha:1]];
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
}];
}
您需要在每个视图上使用单独的手势。
在您的自定义视图中添加另一个 UISwipeGestureRecognizer,方向设置为向下并调用 closebox 方法。
UISwipeGestureRecognizer *swipeGestDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeBox)];
[swipeGestDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[yourCustomView addGestureRecognizer:swipeGestDown];
要完成这项工作,您需要做两件事:
首先,您的 UIView
子类没有 frame
或 bounds
,因为您使用的是 init
而不是 initWithFrame:
其次,您必须在主 UIViewController
上设置一个 delegate
并实现 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
方法。这有效:
在UISummaryCharView.m
#import "UISummaryChartView.h"
@implementation UISummaryChartView
@synthesize mainView;
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super initWithFrame:theView.frame]; // THIS IS IMPORTANT
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
CGFloat startx = 2;
CGFloat starty = 0;
self.width = self.viewWidth - 10;
self.height = (self.viewHeight / 100) * 50;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
NSLog(@"frame %@",NSStringFromCGRect(self.frame));
NSLog(@"bounds %@",NSStringFromCGRect(self.bounds));
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(0, 10, self.mainView.frame.size.width, 1);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 setBackgroundColor:[UIColor colorWithRed:(float)126/255.0 green:(float)35/255.0 blue:(float)32/255.0 alpha:1]];
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
NSLog(@"frame %@",NSStringFromCGRect(self.frame));
NSLog(@"bounds %@",NSStringFromCGRect(self.bounds));
}];
}
-(void)closeBox{
NSLog(@"close box");
// your closeBox method
}
@end
在Parent.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.chartView = [[UISummaryChartView alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.chartView openBox];
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];
[swipeGest setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addSubview:self.chartView];
self.chartView.userInteractionEnabled = YES;
[self.chartView addGestureRecognizer:swipeGest];
swipeGest.delegate = self;
}
-(void)swipeDetected: (UISwipeGestureRecognizer *) sender{
NSLog(@"swiped..");
if(self.chartView.frame.size.height > 50){
//chart view is open
NSLog(@"closing");
[self.chartView closeBox];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(@"shouldReceiveTouch?");
return YES;
}