【问题标题】:iOS Objective C ScrollViewiOS 目标 C 滚动视图
【发布时间】:2018-12-25 12:11:18
【问题描述】:

我正在尝试实现一个 UIScrollView 并使用 Objective-C 从 Xcode 中的图像数组中加载图像,UIScrollView 中的每个图像在纵向和横向模式下都必须是全屏的。我有能够使其在纵向模式下工作,但不能在横向模式下工作。它应该在所有 iOS 设备尺寸下都是全屏的。以下是我到目前为止编写的代码。我的故事板中有 UIScrollView、一个按钮和一个标签。任何答案或指向实现这一点的教程将不胜感激。提前致谢。

CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat widthInPixel = screen.size.width;
CGFloat heightInPixel = screen.size.height;
float increaseAmount = widthInPixel;
self.imageScrollView.contentMode = UIViewContentModeScaleAspectFit;
self.imageScrollView.pagingEnabled = YES;
[self.imageScrollView setAlwaysBounceVertical:NO];
[self.imageScrollView setAlwaysBounceHorizontal:NO];
imageViews = [[NSMutableArray alloc] init];
self.imageScrollView.clipsToBounds = YES;
NSInteger imageNumbers  = [self.images count];
UIImageView *image;
for(NSInteger i = 0; i < imageNumbers; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;
    image = [[UIImageView alloc] initWithFrame:
                          CGRectMake(xOrigin, 0,
                                     widthInPixel,

    self.imageScrollView.frame.size.height)];



    image.contentMode = UIViewContentModeScaleAspectFit;
    image.clipsToBounds = YES;
    image.image = self.images[i];

    [image setAutoresizingMask:
     UIViewAutoresizingFlexibleWidth |
     UIViewAutoresizingFlexibleHeight];

    [self.imageScrollView addSubview:image];
}

self.imageScrollView.contentSize = CGSizeMake(image.frame.size.width *
                                         imageNumbers,

self.imageScrollView.frame.size.height);

【问题讨论】:

  • 为什么不使用滚动视图而不是使用 UICollectionView,这会减少很多代码。
  • @NisarAhmad,我需要用 UIScrollView 来实现它,如果您有任何其他建议,请告诉我,谢谢,不胜感激
  • 相对于滚动视图以编程方式向 imageView 添加前导、尾随、顶部和底部约束
  • happyteamlabs.com/blog/…你可以相应地点击这个链接
  • @ucheGodfrey - 查看我编辑的答案...

标签: ios objective-c uiscrollview


【解决方案1】:

您真的应该学习如何使用自动布局和约束。使用您最喜欢的搜索引擎并搜索ios auto layout tutorial ...您会找到大量资料。


编辑:

在启用 分页 的情况下旋转滚动视图时,滚动偏移是一个固有问题。有关viewWillTransitionToSize 的实现,请参阅下面的编辑。


但是,给你一个想法,这将做你想要的,包括在设备旋转时自动调整大小:

//
//  ViewController.m
//  ScrollingImages
//
//  Created by Don Mag on 7/19/18.
//

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIScrollView *theScrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *images = @[@"a", @"b", @"c", @"d", @"e"];

    [_theScrollView setPagingEnabled:YES];
    [_theScrollView setAlwaysBounceVertical:NO];
    [_theScrollView setAlwaysBounceHorizontal:NO];

    // we'll use this to hold the most recently added view
    UIImageView *prevImageView = nil;

    for (int i = 0; i < images.count; i++) {

        // create an image view with named image from array
        UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageNamed:images[i]]];

        // we want to use auto-layout
        v.translatesAutoresizingMaskIntoConstraints = NO;

        // we want aspect-fit
        v.contentMode = UIViewContentModeScaleAspectFit;

        // add it to the scroll view
        [_theScrollView addSubview:v];

        // set width and height constraints equal to the scroll view
        [[NSLayoutConstraint
          constraintWithItem:v
          attribute:NSLayoutAttributeWidth
          relatedBy:NSLayoutRelationEqual
          toItem:_theScrollView
          attribute:NSLayoutAttributeWidth
          multiplier:1.0
          constant:0.0] setActive:YES];

        [[NSLayoutConstraint
          constraintWithItem:v
          attribute:NSLayoutAttributeHeight
          relatedBy:NSLayoutRelationEqual
          toItem:_theScrollView
          attribute:NSLayoutAttributeHeight
          multiplier:1.0
          constant:0.0] setActive:YES];

        if (i == 0) {  // if it's the first image

            // add top constraint
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeTop
              relatedBy:NSLayoutRelationEqual
              toItem:_theScrollView
              attribute:NSLayoutAttributeTop
              multiplier:1.0
              constant:0.0] setActive:YES];

            // and leading constraint
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeLeading
              relatedBy:NSLayoutRelationEqual
              toItem:_theScrollView
              attribute:NSLayoutAttributeLeading
              multiplier:1.0
              constant:0.0] setActive:YES];

        } else {

            // constrain leading to previous image view trailing
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeLeading
              relatedBy:NSLayoutRelationEqual
              toItem:prevImageView
              attribute:NSLayoutAttributeTrailing
              multiplier:1.0
              constant:0.0] setActive:YES];

            // and top to previous image view top
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeTop
              relatedBy:NSLayoutRelationEqual
              toItem:prevImageView
              attribute:NSLayoutAttributeTop
              multiplier:1.0
              constant:0.0] setActive:YES];

        }

        if (i == images.count - 1) {  // if it's the last image

            // add trailing constraint
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeTrailing
              relatedBy:NSLayoutRelationEqual
              toItem:_theScrollView
              attribute:NSLayoutAttributeTrailing
              multiplier:1.0
              constant:0.0] setActive:YES];

            // and bottom constraint
            [[NSLayoutConstraint
              constraintWithItem:v
              attribute:NSLayoutAttributeBottom
              relatedBy:NSLayoutRelationEqual
              toItem:_theScrollView
              attribute:NSLayoutAttributeBottom
              multiplier:1.0
              constant:0.0] setActive:YES];

        }

        // reference to most recently added view
        prevImageView = v;

    }

}

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // execute before rotation

    // get the "index" of the current image in the scroll view
    NSUInteger idx = (unsigned)(_theScrollView.contentOffset.x / _theScrollView.frame.size.width);

    [coordinator animateAlongsideTransition:^(id  _Nonnull context) {
        // execute during rotation

        // update the scroll view's contentOffset, based on the "index"
        self.theScrollView.contentOffset = CGPointMake(idx * self.theScrollView.frame.size.width, 0);

    } completion:^(id  _Nonnull context) {
        // execute after rotation (if additional code wanted)
    }];

}
@end

您可以在此处下载一个工作示例项目:https://github.com/DonMag/ScrollingImages

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2017-05-26
    • 2013-09-09
    • 1970-01-01
    相关资源
    最近更新 更多