iOS基于UIScrollView实现滑动引导页
2017-01-24 09:46
1081 查看
上代码前,我们先来看下实现的效果图:
WelcomeViewController.h
#import <UIKit/UIKit.h> @interface WelcomeViewController : UIViewController @end
WelcomeViewController.m
#import "WelcomeViewController.h" #define IMAGECOUNT 3 @interface WelcomeViewController () <UIScrollViewDelegate> @property (nonatomic, strong)UIPageControl *pageControl; @end @implementation WelcomeViewController - (void)viewDidLoad { [super viewDidLoad]; //创建ScrollView UIScrollView *sv = [[UIScrollView alloc] init]; sv.frame = self.view.bounds; //设置边缘不弹跳 sv.bounces = NO; //整页滚动 sv.pagingEnabled = YES; sv.showsHorizontalScrollIndicator = NO; //加入多个子视图(ImageView) for(NSInteger i=0; i<IMAGECOUNT; i++){ NSString *imgName = [NSString stringWithFormat:@"%ld", i+1]; UIImage *image = [UIImage imageNamed:imgName]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; CGRect frame = CGRectZero; frame.origin.x = i * sv.frame.size.width; frame.size = sv.frame.size; imageView.frame = frame; [sv addSubview:imageView]; if(i==IMAGECOUNT-1){ //开启图片的用户点击功能 imageView.userInteractionEnabled = YES; //加个按钮 UIButton *button = [[UIButton alloc]init]; button.frame = CGRectMake((imageView.frame.size.width-150)/2, imageView.frame.size.height*0.8, 150, 40); button.backgroundColor = [UIColor orangeColor]; [button setTitle:@"立即体验" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont boldSystemFontOfSize:16]; [imageView addSubview:button]; [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside]; } } sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height); [self.view addSubview:sv]; //加入页面指示控件PageControl UIPageControl *pageControl = [[UIPageControl alloc]init]; self.pageControl = pageControl; //设置frame pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20); //分页面的数量 pageControl.numberOfPages = IMAGECOUNT; //设置小圆点渲染颜色 pageControl.pageIndicatorTintColor = [UIColor whiteColor]; //设置当前选中小圆点的渲染颜色 pageControl.currentPageIndicatorTintColor = [UIColor redColor]; //关闭用户点击交互 pageControl.userInteractionEnabled = NO; [self.view addSubview:pageControl]; sv.delegate = self; } - (void)enter { NSLog(@"进入应用"); } //UIScrollViewDelegate方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; if(offset.x<=0){ offset.x = 0; scrollView.contentOffset = offset; } NSUInteger index = round(offset.x / scrollView.frame.size.width); self.pageControl.currentPage = index; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- ios基于UIScrollView实现滑动引导页
- iOS使用UIScrollView实现左右滑动UITableView和UICollectionView
- iOS使用UIScrollView实现左右滑动UITableView和UICollectionView
- [iOS]通过UIScrollView和UIPageControl实现滑动切换的效果
- iOS中UIScrollView嵌套UIImageView实现图片滑动浏览、缩放
- iOS-实现UIScrollview的无限循环滑动(转载自Kenshin Cui's Blog)不过感觉快速滑动还是有缺陷啊
- ios-day05-03(实现UIScrollViewDelegate协议,监听ScrollView的缩放、滑动等状态)
- 实现在ios开发中的App滑动封面 UIScrollView
- IOS--UiScrollView和UIPageControl实现滑动翻页
- ios UIScrollView 循环滑动
- IOS下省市区选择器实现[基于TableView]
- ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar
- iphone ios UIScrollView 机制及实现
- iOS开发:UIScrollView如何禁止上下滑动
- IOS之左右滑动效果的实现——利用PPRevealViewController
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- IOS之左右滑动效果的实现——利用SWRevealViewController
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- iOS UIScrollView 停止滑动 减速
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)