我是Objective-C的新手。我创建UIScrollView
对象并使用以下代码添加到我的视图中:
height = self.view.frame.size.height;
width = self.view.frame.size.width;
scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
scrollbar.delegate = self;
scrollbar.backgroundColor = [UIColor whiteColor];
scrollbar.maximumZoomScale = 1.0;
scrollbar.minimumZoomScale = 1.0;
scrollbar.clipsToBounds = YES;
scrollbar.showsHorizontalScrollIndicator = YES;
scrollbar.pagingEnabled = YES;
[scrollbar setContentSize:CGSizeMake(width*4, height*4)];
[self.view addSubview:scrollbar];
for (int i = 1; i <= 4; i++) {
first = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
first.view.frame = CGRectMake((i-1)*width, 0, width, height*4);
[scrollbar addSubview:first.view];
switch (i) {
ase 1:
first.view.backgroundColor = [UIColor blueColor];
break;
case 2:
first.view.backgroundColor = [UIColor redColor];
break;
case 3:
first.view.backgroundColor = [UIColor greenColor];
break;
case 4:
first.view.backgroundColor = [UIColor grayColor];
break;
default:
break;
}
}
在我的代码中,我现在想在ScrollView中添加4种不同颜色的视图,当我在ScrollView上滚动时,想要检测dx和dy(dx:Axis.x上的行驶距离,而dy:Axis.y上的行驶距离)并检查这两个变量,并什么时候 :
Notic:我想在ScrollView上的任一触摸和Axis(x或y)上的移动触摸或在Both Axis(x和y)上的触摸上进行检查时:
如果(dx> dy)禁用水平滚动并沿垂直方向移动!!!否则在水平方向移动并禁用垂直滚动!!!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect visibleRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.contentOffset.x + scrollView.bounds.size.width, scrollView.contentOffset.y + scrollView.bounds.size.height);
NSLog(@"%f,%f",visibleRect.origin.x,visibleRect.origin.y);
/*NSLog(@"x : %f",scrollView.contentOffset.x);
NSLog(@"y : %f",scrollView.contentOffset.y);*/
if (fabsf(scrollView.contentOffset.x) > fabsf(scrollView.contentOffset.y)) {
NSLog(@"Vertical Side");
}
else {
NSLog(@"Horizontal Side");
}
}
请指导我。我不能禁用一侧并移动另一侧!!!谢谢
您可以实现想要将一些代码添加到委托中的结果。这是我的ViewController.m
档案 -viewDidLoad
,#import
语句和其他方法被省略。
@interface ViewController () {
CGPoint initialOffset;
NSInteger direction; // 0 undefined, 1 horizontal, 2 vertical
}
@end
@implementation ViewController
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// retrieve current offset
CGPoint currentOffset = scrollView.contentOffset;
// do we know which is the predominant direction?
if (direction == 0) {
// no.
CGFloat dx = currentOffset.x - initialOffset.x;
CGFloat dy = currentOffset.y - initialOffset.y;
// we need to decide in which direction we are moving
if (fabs(dx) >= fabs(dy)) {
direction = 1; // horizontal
} else if (fabs(dy) > fabs(dx)) {
direction = 2;
}
}
// ok now we have the direction. update the offset if necessary
if (direction == 1 && currentOffset.y != initialOffset.y) {
// remove y offset
currentOffset.y = initialOffset.y;
// update
[scrollView setContentOffset:currentOffset];
} else if (direction == 2 && currentOffset.x != initialOffset.x) {
currentOffset.x = initialOffset.x;
[scrollView setContentOffset:currentOffset];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// store the current offset
initialOffset = scrollView.contentOffset;
// reset flag
direction = 0; // AKA undefined
}
@end
当您开始拖动时,委托会将标志重置direction
为“未知”状态,并存储当前内容偏移量。每次拖动动作后,您的-scrollViewDidScroll:
名字就会被调用。在那里,您确定哪个方向是主要方向(如果尚未完成),然后通过删除x(或y)偏移量来相应地校正当前滚动偏移量。
我使用您提供的相同设置对此进行了测试,只是我使用了UIImageView
内部结构,UIScrollView
并通过InterfaceBuilder进行了所有设置,但是应该可以正常工作。从理论上讲,使用此方法可以替换directionLock
,但是请记住,-scrollViewDidScroll
在操作期间多次调用该方法,并且每次重写内容偏移量时(如果在两个方向上都发生滚动)。因此,如果保持directionLock
启用状态,则可以保存setContentOffset:
委托执行的许多调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句