图像滑块,上一个和下一个图像可见

时间:2015-06-21 11:33:59

标签: ios objective-c uiscrollview slider image-gallery

我所拥有的是图像列表。我在滚动视图中显示这些图像并启用了分页。

现在客户回来要求显示下一个(部分可见),当前(完全可见)和前一个(部分可见)图像,如下图所示。

http://a5.mzstatic.com/us/r30/Purple5/v4/48/db/b6/48dbb6af-bd12-2380-2fcb-061beb81e739/screen568x568.jpeg

我尝试的内容如下。

int mm = 150;
for (int i=0;i<featuredProductArray.count;i++) {
    UIButton *mButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [mButton addTarget:self action:@selector(takeMeToProductDetails:) forControlEvents:UIControlEventTouchUpInside];
    mButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [mButton sd_setImageWithURL:[NSURL URLWithString:[[[featuredProductArray objectAtIndex:i] valueForKey:@"Image"] stringByReplacingOccurrencesOfString:@"/Original/" withString:@"/1080/"] ] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"slider_bg.png"]];
    [mButton setAdjustsImageWhenHighlighted:NO];
    mButton.accessibilityValue = [NSString stringWithFormat:@"feat%d", i];
    mButton.frame = CGRectMake(mm*iPhoneFactorX, 0, 780*iPhoneFactorX, iPhoneHeight-(20+(149*iPhoneFactorX)));
    mm = mm + 780+50;
    [yScrollView addSubview:mButton];
}

现在我有分页问题..当我滚动时,第二张图片没有居中...

2 个答案:

答案 0 :(得分:1)

要制作pagingEnabled,您必须确保页面连续且大小相同。有必要增加子视图的大小以包括按钮之间距离的一半(可能通过创建包含按钮的超视图)。

答案 1 :(得分:0)

首先禁用从IB滚动。

enter image description here

现在使用for循环

将彼此相邻的图像添加到一起

设置scrollview contentSize

创建变量int myPostForSwipe并初始化为0

现在添加滑动手势,这非常重要

UISwipeGestureRecognizer *sswipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeScreen:)];
sswipe.direction = UISwipeGestureRecognizerDirectionLeft;
sswipe.delegate = self;
[yScrollView addGestureRecognizer:sswipe];

sswipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeScreen:)];
sswipe.direction = UISwipeGestureRecognizerDirectionRight;
sswipe.delegate = self;
[yScrollView addGestureRecognizer:sswipe];

实施didSwipeScreen

- (void)didSwipeScreen:(UISwipeGestureRecognizer *)gesture
{
    NSLog(@"didSwipeScreen");
    switch (gesture.direction) {
            NSLog(@"gesture.direction");
        case UISwipeGestureRecognizerDirectionUp:
            // you can include this case too
            NSLog(@"UISwipeGestureRecognizerDirectionUp");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            // you can include this case too
            NSLog(@"UISwipeGestureRecognizerDirectionDown");
            break;
        case UISwipeGestureRecognizerDirectionLeft:

            if (myPostForSwipe<(featuredProductArray.count-1)) {
                myPostForSwipe = myPostForSwipe + 1;
                float myX1 = (myPostForSwipe*830*iPhoneFactorX);
                NSLog(@"UISwipeGestureRecognizerDirectionLeft==%f===%d", myX1, myPostForSwipe);
                [yScrollView setContentOffset:CGPointMake(myX1, 0) animated:YES];
            }
            break;
        case UISwipeGestureRecognizerDirectionRight:
            if (myPostForSwipe>=1) {
                myPostForSwipe = myPostForSwipe - 1;
                NSLog(@"UISwipeGestureRecognizerDirectionRight==%d", myPostForSwipe);
                float myX2 = (myPostForSwipe*830*iPhoneFactorX);
                [yScrollView setContentOffset:CGPointMake(myX2, 0) animated:YES];
            }
            break;
        default:
            break;
    }
}

就是这样

另一种解决方案

另一种解决方案越来越简单......

  1. 创建中间图像大小的UIScrollView

  2. 取消勾选剪辑子视图&amp; tick Paging Enable(这非常重要)

  3. 现在用图片填充scrollview。

  4. 你完成了!!!

    这一点对我不起作用bcz我有侧面菜单&amp;所有的滚动视图都在侧面菜单上传播。

    如果有人有任何问题,请告诉我。