轮播图

2017-09-21  本文已影响0人  哈喽Mm

#import "ViewController.h"#import "SecViewController.h"@interface ViewController (){

//实现三个属性

UIScrollView *scroll;

NSArray *imgArr;//图片

UIPageControl *page;//页面

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//self.view.backgroundColor = [UIColor whiteColor];

scroll = [[UIScrollView alloc]initWithFrame:self.view.frame];

//添加协议

scroll.delegate = self;

[self.view addSubview:scroll];

imgArr = @[@"guide1",@"guide2",@"guide3",@"guide4",];

//设置图片进行滚动

scroll.pagingEnabled = YES;

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

{

UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(scroll.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];

img.userInteractionEnabled = YES;

if (i == imgArr.count - 1)

{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:@"立即体验" forState:UIControlStateNormal];

btn.frame = CGRectMake((self.view.frame.size.width -100) / 2 , 600 , 100, 44);

[btn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];

btn.layer.cornerRadius = 10;

btn.layer.masksToBounds = YES;

btn.backgroundColor = [UIColor redColor];

[img addSubview:btn];

}

img.image = [UIImage imageNamed:imgArr[i]];

[scroll addSubview:img];

}

scroll.contentSize = CGSizeMake(self.view.frame.size.width * imgArr.count, self.view.frame.size.height);

//取消弹簧效果

scroll.bounces = NO;

//隐藏滚动条

scroll.showsHorizontalScrollIndicator = NO;

//页码  (点)

page = [[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width -100) / 2, 536, 100, 20)];

page.numberOfPages = imgArr.count;

//设置初始页码

page.currentPage = 0;

//设置页码的颜色

page.pageIndicatorTintColor = [UIColor yellowColor];

[self.view addSubview:page];

}

#pragma -

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

NSLog(@"contentOffset: x: %f,y: %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

//求余

NSInteger index = scroll.contentOffset.x / scrollView.frame.size.width;

page.currentPage = index;

}

- (void)Click

{

SecViewController *mc = [[SecViewController alloc]init];

[self presentViewController:mc animated:YES completion:^{

}];

}

上一篇 下一篇

猜你喜欢

热点阅读