gpt4 book ai didi

ios - 如何以编程方式创建带有水平图像的 ScrollView ?

转载 作者:行者123 更新时间:2023-11-29 01:24:49 26 4
gpt4 key购买 nike

嗨,我是 iOS 的新手,如何以编程方式创建带有图像的 ScrollView

像这样我试过了,我得到的是 ScrollView ,图像是水平的,但图像的放置方式如下图所示

我的做法是否正确?

请帮帮我。谢谢

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// CGRectMake(0, 0, 600, 200);
_scrollView.tag = 1;
_scrollView.autoresizingMask=UIViewAutoresizingNone;
NSLog(@"--->%@",_scrollView);

[self.view addSubview:_scrollView];
[self setupScrollView:_scrollView];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 154, 480, 36)];
NSLog(@"--->%@",pgCtr);
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];

// Do any additional setup after loading the view, typically from a nib.
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.

for (int i=1; i<=3; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"abc%d.png",i]];

// create imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];

// set scale to fill
_imageView.contentMode=UIViewContentModeScaleToFill;
// set image
[_imageView setImage:image];
// apply tag to access in future
_imageView.tag=i+1;
NSLog(@"--->%@",_imageView);
// add to scrollView
[scrMain addSubview:_imageView];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(320*3, scrMain.frame.size.height)];
NSLog(@"--->%@",scrMain);
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];


}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
NSLog(@"--->%@",scrMain);

// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
NSLog(@"--->%@",pgCtr);

// get the current offset ( which page is being displayed )
CGFloat contentOffset = 194;
NSLog(@"------------------->%f",contentOffset);
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
NSLog(@"%d",nextPage);
// if page is not 10, display it
if( nextPage!=10 ) {


[scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
NSLog(@"--->%@",scrMain);
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)


} else {

[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
NSLog(@"-------------------------------------------->%@",scrMain);
pgCtr.currentPage=0;
}
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

我认为,你在这一行是错误的:

// create imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];

应该是:

// create imageView // change height to width
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.width+5), 0, 320, 195)];

关于ios - 如何以编程方式创建带有水平图像的 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com