我查看了其他解决方案,但我找不到适合自己的解决方案。我有一个UIImageView
inside a UIScrollView
,我在其中展示大图。
我在我UIScrollView
的左右滑动手势识别器中启用了捏合手势。
现在,scrollview 的平移手势似乎禁用(或损坏)滑动手势。我不想在我的 上禁用水平或垂直滚动UIScrollView
,并且我的照片的初始缩放太大而无法禁用水平滚动。
我想要做的是当我来到 UIScrollView 的边缘时触发滑动手势。
这里有一些代码;
- (void)viewDidLoad
{
// recognizer for pinch gestures
UIPinchGestureRecognizer *pinchRecognizer =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.myScrollView addGestureRecognizer:pinchRecognizer];
[self.myScrollView setClipsToBounds:NO];
// recognizer for swipe gestures
UISwipeGestureRecognizer *recognizer;
// left and right swipe recognizers for left and right animation
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self myScrollView] addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self myScrollView] addGestureRecognizer:recognizer];
....
我的左滑动处理程序,目前左右滑动没有任何附加功能
-(void)handleLeftSwipe:(UISwipeGestureRecognizer *)recognizer
{
if(!self.tableView.hidden) self.tableView.hidden = YES;
[self showRequiredStuff];
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
我的初始屏幕尺寸;
#define IMAGE_VIEW_WIDTH 320.0
#define IMAGE_VIEW_HEIGHT 384.0
我对图片使用缩放,使它们尽可能小,但大多数是宽图像,在这种情况下启用水平滚动,禁用垂直滚动。虽然我的滑动处理程序也是水平的。
我想我已经清楚地解释了发生了什么以及我需要什么。我发布了代码,因为我是 iphone 应用程序开发的新手,我都想帮助其他人尽可能多地看到代码,也许有人会指出任何不好的编程,我们都会从中受益。
相关解决方案的其他发现;设置后;
@interface myViewController () <UIScrollViewDelegate>
和
self.myScrollView.delegate = self;
水平检测是否到达边缘
- (BOOL) hasReachedAHorizontalEdge {
CGPoint offset = self.myScrollView.contentOffset;
CGSize contentSize = self.myScrollView.contentSize;
CGFloat height = self.myScrollView.frame.size.height;
CGFloat width = self.myScrollView.frame.size.width;
if ( offset.x == 0 ||
(offset.x + width) == contentSize.width ) {
return YES;
}
return NO;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
if ( [self hasReachedAHorizontalEdge] ) {
NSLog(@"Reached horizontal edge.");
// required here
}
}
此时,我只需要禁用到达末端的滚动,等等。如果我到达滚动的右边缘,我只需要禁用右滚动,这样就会触发滑动。