【问题标题】:image slider with previous and next image visible显示上一张和下一张图像的图像滑块
【发布时间】:2015-06-21 11:33:58
【问题描述】:

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

现在客户回来询问是否显示下一张(部分可见)、当前(完全可见)和上一张(部分可见)图像,如下图所示。


(来源:mzstatic.com

我尝试如下。

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];
}

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

【问题讨论】:

  • 显示您设置分页的位置。
  • @Mundi : 分页只需写yScrollView.pagingEnabled = YES;

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    首先禁用 IB 的滚动。

    现在使用 for 循环将图像彼此相邻添加

    设置滚动视图内容大小

    创建变量 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. 取消勾选 Clip Subviews 并勾选 Paging Enable(这很重要)

    3. 现在用图像填充滚动视图。

    你已经完成了!!!

    这点对我不起作用,因为我有侧边菜单,所有的滚动视图都在侧边菜单上展开。

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

    【讨论】:

      最近更新 更多