iOS基于UIScrollView實(shí)現(xiàn)滑動引導(dǎo)頁
更新時間:2022年02月15日 15:46:08 作者:chenzheng8975
這篇文章主要為大家詳細(xì)介紹了iOS基于UIScrollView實(shí)現(xiàn)滑動引導(dǎo)頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
上代碼前,我們先來看下實(shí)現(xiàn)的效果圖:

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];
//創(chuàng)建ScrollView
UIScrollView *sv = [[UIScrollView alloc] init];
sv.frame = self.view.bounds;
//設(shè)置邊緣不彈跳
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){
//開啟圖片的用戶點(diǎn)擊功能
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:@"立即體驗(yàn)" 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;
//設(shè)置frame
pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20);
//分頁面的數(shù)量
pageControl.numberOfPages = IMAGECOUNT;
//設(shè)置小圓點(diǎn)渲染顏色
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
//設(shè)置當(dāng)前選中小圓點(diǎn)的渲染顏色
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//關(guān)閉用戶點(diǎn)擊交互
pageControl.userInteractionEnabled = NO;
[self.view addSubview:pageControl];
sv.delegate = self;
}
- (void)enter
{
NSLog(@"進(jìn)入應(yīng)用");
}
//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 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 模仿iOS版微信的滑動View效果
- iOS使用pageViewController實(shí)現(xiàn)多視圖滑動切換
- iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動卡頓問題解決的方法
- iOS開發(fā)上下滑動UIScrollview隱藏或者顯示導(dǎo)航欄的實(shí)例
- ios scrollview嵌套tableview同向滑動的示例
- Android仿IOS ViewPager滑動進(jìn)度條
- IOS仿今日頭條滑動導(dǎo)航欄
- iOS滑動解鎖、滑動獲取驗(yàn)證碼效果的實(shí)現(xiàn)代碼
- iOS 頁面滑動與標(biāo)題切換顏色漸變的聯(lián)動效果實(shí)例
- iOS自定義View實(shí)現(xiàn)卡片滑動
相關(guān)文章
快速解決iOS10不能跳轉(zhuǎn)系統(tǒng)WiFi列表的問題
下面小編就為大家?guī)硪黄焖俳鉀QiOS10不能跳轉(zhuǎn)系統(tǒng)WiFi列表的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
IOS self和super詳解實(shí)現(xiàn)原理及區(qū)別
這篇文章主要介紹了iOS self和super詳解實(shí)現(xiàn)原理及區(qū)別的相關(guān)資料,這里不僅說明區(qū)別并介紹實(shí)現(xiàn)原理,具有參考價值,需要的朋友可以參考下2016-12-12
Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
ios 流媒體播放器實(shí)現(xiàn)流程及FreeStreamer的使用的示例
本篇文章主要介紹了ios 流媒體播放器實(shí)現(xiàn)流程及FreeStreamer的使用的示例代碼,非常具有實(shí)用價值,需要的朋友可以參考下2018-01-01

