简体   繁体   English

iOS Objective C ScrollView

[英]iOS Objective C ScrollView

在此处输入图片说明 I am trying to implement a UIScrollView and load it with images from an array of images in Xcode using objective-C, each image in the UIScrollView must be full screen both in portrait and in the landscape mode.I have been able to make it work in portrait mode but not in landscape mode. 我正在尝试实现UIScrollView并使用Objective-C从Xcode中的图像数组中加载图像,UIScrollView中的每个图像在纵向和横向模式下都必须全屏显示。我已经能够使其工作在纵向模式下,但不在横向模式下。 It should be fullscreen in all iOS device sizes. 所有iOS设备尺寸均应为全屏显示。 Below is the code I have written so far. 以下是我到目前为止编写的代码。 I have UIScrollView in my storyboard, a button and a label. 我的情节提要中有UIScrollView,一个按钮和一个标签。 Any answer or pointing to a tutorial that implements this will be appreciated. 任何答案或指向实现此目的的教程将不胜感激。 Thanks in advance. 提前致谢。

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);

You really should learn how to use auto-layout and constraints. 您确实应该学习如何使用自动布局和约束。 Use your favorite search engine and search for ios auto layout tutorial ... you'll find plenty of material. 使用您喜欢的搜索引擎并搜索ios auto layout tutorial ...,您会发现很多材料。


Edit: 编辑:

Scroll offset is an inherent issue when rotating a scroll view with paging enabled. 在启用分页的情况下旋转滚动视图时,滚动偏移是一个固有的问题。 See the edit below for an implementation of viewWillTransitionToSize . 有关viewWillTransitionToSize的实现,请参见下面的编辑。


But, to give you an idea, this will do what you want, including auto-resizing on device rotation: 但是,给您一个想法,这会做您想要的,包括在设备旋转时自动调整大小:

//
//  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

You can download a working example project here: https://github.com/DonMag/ScrollingImages 您可以在此处下载一个有效的示例项目: https : //github.com/DonMag/ScrollingImages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM