scrollView与滑动返回冲突问题(完美解决)
2017-09-06 本文已影响17人
吃货_X
- (UIScrollView *)bgView{
if (!_bgView) {
_bgView =({
UIScrollView *scrollV = [[UIScrollView alloc] init];
scrollV.frame = [UIScreen mainScreen].bounds;
scrollV.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[scrollV addGestureRecognizer:tapBg];
scrollV.bouncesZoom = YES;
scrollV.delegate = self;
scrollV.pagingEnabled = YES;
scrollV.bounces = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
[pan addTarget:self.navigationController.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];//方法不用实现,是系统的滑动返回方法。
//self.navigationController.interactivePopGestureRecognizer.enabled = NO; 禁用系统的滑动返回。
pan.delegate = self;
[scrollV addGestureRecognizer:pan];//添加手势
scrollV;
});
}
return _bgView;
}
冲突解决方法
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint point = [self.bgView.panGestureRecognizer velocityInView:self.view];
CGFloat width = self.bgView.contentOffset.x;
if (point.x < 0) {
return NO;
} else if(width == 0){
return YES;
}
return NO;
}
所有代码
.h
#import <UIKit/UIKit.h>
@interface HomeImagViewController : UIViewController
@property (nonatomic, strong) NSArray *array;
@end
.m
#import "HomeImagViewController.h"
@interface HomeImagViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIScrollView *bgView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *text;
@end
@implementation HomeImagViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:YES];
self.view.backgroundColor = COLOR(whiteColor);
[self getUi];
}
- (void)back{
NavBack(YES);
}
- (void)getUi{
self.bgView.contentSize = CGSizeMake(MAINSCREEN_WIDTH * self.array.count, 0);
for (int i = 0; i < self.array.count; i++) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.array[i]]];
__block UIImage *image2 = [[UIImage alloc]init];
__block UIImageView *image1 = [[UIImageView alloc]init];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
CGFloat y = self.bgView.frame.size.height / 2 - (MAINSCREEN_WIDTH * image2.size.height / image2.size.width)/2;
dispatch_sync(dispatch_get_main_queue(), ^{
image1.frame = CGRectMake(i * MAINSCREEN_WIDTH , y, MAINSCREEN_WIDTH, MAINSCREEN_WIDTH * image2.size.height / image2.size.width);
image1.image = image2;
[self.bgView addSubview:image1];
});
});
}
self.bgView.contentOffset = CGPointMake(0, 0);
[self.view addSubview:self.bgView];
[self.view addSubview:self.button];
[self.view addSubview:self.label];
[self.view addSubview:self.text];
[self.text mas_makeConstraints:^(MASConstraintMaker *make) {
MLEFT(0);
MRIGHT(0);
MBOTTON(0);
MHEIGHT(100);
}];
self.text.text = @"光明网过户死阿里看的见覅偶按时都会发生了肯定积分拉克神界大陆手拉手京东方怕时间的房价噗噗;‘;看就看哟啊;,功能片;都浪费啊;、。东南啊;sd/fl'sa;d.maldk.,k房;";//随便写的
}
- (NSAttributedString *)string:(NSString *)str{
NSMutableAttributedString *ss = [[NSMutableAttributedString alloc]initWithString:str];
NSRange rang = NSMakeRange(0, 1);
[ss addAttribute:NSFontAttributeName value:FONT(50) range:rang];
return ss;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat width = scrollView.contentOffset.x;
NSInteger nu = width / MAINSCREEN_WIDTH + 1;
self.label.attributedText = [self string:[NSString stringWithFormat:@"%ld/%ld",nu,self.array.count]];
}
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint point = [self.bgView.panGestureRecognizer velocityInView:self.view];
CGFloat width = self.bgView.contentOffset.x;
if (point.x < 0) {
return NO;
} else if(width == 0){
return YES;
}
return NO;
}
- (void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
//想干嘛干嘛
}
#pragma mark - Getter/Setter
- (UIScrollView *)bgView{
if (!_bgView) {
_bgView =({
UIScrollView *scrollV = [[UIScrollView alloc] init];
scrollV.frame = [UIScreen mainScreen].bounds;
scrollV.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
[scrollV addGestureRecognizer:tapBg];
scrollV.bouncesZoom = YES;
scrollV.delegate = self;
scrollV.pagingEnabled = YES;
scrollV.bounces = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
[pan addTarget:self.navigationController.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
pan.delegate = self;
[scrollV addGestureRecognizer:pan];
scrollV;
});
}
return _bgView;
}
- (NSArray *)array{
if (!_array) {
_array = [NSArray array];
}
return _array;
}
- (UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]init];
_label.textColor = COLOR(whiteColor);
_label.textAlignment = NSTextAlignmentCenter;
_label.font = FONT(30);
_label.frame = CGRectMake(30, 22, MAINSCREEN_WIDTH - 60, 44);
_label.attributedText = [self string:[NSString stringWithFormat:@"1/%ld",self.array.count]];
}
return _label;
}
- (UILabel *)text{
if (!_text) {
_text = [[UILabel alloc]init];
_text.textColor = COLOR(whiteColor);
_text.font = FONT(30);
_text.numberOfLines = 0;
_text.backgroundColor = GetColor(1, 1, 1, 0.3);
}
return _text;
}
- (UIButton *)button{
if (!_button) {
_button = [[UIButton alloc]init];
[_button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
_button.frame = CGRectMake(10, 29, 30, 30);
[_button setImage:IMGNAME(@"返回") forState:UIControlStateNormal];
}
return _button;
}
@end