现如今新闻类的APP一般都有轮播图,最常见的设计就是给轮播图一个计时器,让其自己每隔几秒轮播到下一张。但如果用户自行拖拽时计时器就应该失去响应,以用户的手势方法为第一响应;当用户停止手势触发轮播图时,计时器又会开始生效。
新建一个project后,一如既往的先在Delegate.m中设置窗口:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] ;
_window.backgroundColor = [UIColor whiteColor];
_window.rootViewController = [[ViewController alloc] init];
[_window makeKeyAndVisible];
接着,我们要在ViewController里,设置我们需要的工具:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
scrollView.backgroundColor = [UIColor colorWithRed:0.722 green:1.000 blue:0.817 alpha:1.000];
[self.view addSubview:scrollView];
并开始设置scrollView的一些变量
1.内容的范围 VGSizeZero CGSizeMake(0, 0)
contentSize 设置可以滚动的范围
scrollView.contentSize = CGSizeMake(0, 1000);
contentOffset 偏移量,滚动的范围
不设置contentSize,contentOffset也可以改变
那么改变:contentOffset什么变了?
其实 改变contentOffset, 实质上是对scrollView的Bounds进行修改
NSLog(@"改变前frame:%@",NSStringFromCGRect(scrollView.frame));
NSLog(@"改变前bounds:%@",NSStringFromCGRect(scrollView.bounds));
scrollView.contentOffset
= CGPointMake(0, 500);
NSLog(@"改变后frame:%@",NSStringFromCGRect(scrollView.frame));
NSLog(@"改变后bounds:%@",NSStringFromCGRect(scrollView.bounds));
3.contentIset
添加滚动区域四周的滚动范围
scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
4.directionalLockEnabled
锁定垂直或水平滚动(45°角, 锁会失效)
scrollView.directionalLockEnabled = YES;
5. bounces 回弹效果
scrollView.bounces = NO;
6.alawaysBounceVertical
在没有垂直方向的滚动范围时,保证垂直方向的垂直效果存在
scrollView.alwaysBounceVertical = YES;
7,alwaysBounceHorizontal
在没有水平方向的滚动范围时,保证水平方向的垂直效果存在
scrollView.alwaysBounceHorizontal = YES;
8.pagingEnabled 按页滚动
scrollView.pagingEnabled = YES;
9 视图时候能够滚动,默认是YES
scrollView.scrollEnabled = YES;
10.指示条 默认是YES
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;
11. 滚动指示器的边距
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
12.滚动指示器的颜色,背景,深色,浅色,通用
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
13.
tracking 用户是否开始拖动
scrollView.tracking
dragging 用户时候正在拖动
scrollView.dragging
decelerating 用户时候在减速中
scrollView.decelerating
14.滚动视图,双击状态栏是否存在 默认 YES
scrollView.scrollsToTop = YES;
15.scrollView 的代理
scrollView.delegate = self;
16.键盘消失模式
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
我们来测试一下 先设置一个TextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 350, WIDTH - 20, 200)];
textField.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:textField];
17.缩放系数
如果想进行视图缩放,必须实现viewForZoomingInScrollView:
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2;
再来设置一些方法
与其想要放大的子视图绑定,scrollView的contentSize会跟随这个子视图变化
-
(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subviews firstObject];
}
滚动视图滚动,就会执行 -
(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"滚动中");Trackong 用户触摸
if (scrollView.tracking) {
NSLog(@"正在拖拽滚动");} else {
NSLog(@"自己滚动");
}
Dragging 用户开始滑动
if (scrollView.dragging) {
NSLog(@"");
}
Decelerating 用户触摸结束
if (scrollView.decelerating) {
NSLog(@"");
}
}
将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"开始拖拽");
}
将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
}
已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"已经结束拖拽");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
}
以上就是轮播图的大概概念,下面分享一下我做的简易的轮播图:
//
// AppDelegate.m
// 05- HomeWork
//
// Created by 李昊林 on 16/7/20.
// Copyright © 2016年 李昊林. All rights reserved.
//
import "AppDelegate.h"
import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
_window.rootViewController = [[ViewController alloc] init];
[_window makeKeyAndVisible];return YES;
} -
(void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} -
(void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} -
(void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} -
(void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} -
(void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// ViewController.m
// 05 - HomeWork
//
// Created by 李昊林 on 16/7/20.
// Copyright © 2016年 李昊林. All rights reserved.
//
import "ViewController.h"
import "AppDelegate.h"
define FRAME [UIScreen mainScreen].bounds
define WIDTH FRAME.size.width
define HEIGHT FRAME.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIScrollView *scrollViewOfAlbum;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self makeAlbum];
[self addPage];
// Do any additional setup after loading the view, typically from a nib.
// 完善ScrollView相册, 实现轮播及缩放恢复。
// 封装轮播图
// 添加UIPageControl, 绑定事件, 有点击切换效果
// 不用添加定时器
}
- (void)makeAlbum {
UIScrollView *album = [[UIScrollView alloc] initWithFrame:FRAME];
[self.view addSubview:album];
NSArray *arr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", nil];
album.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
album.pagingEnabled = YES;
album.bounces = NO;
album.delegate = self;
for (NSInteger i = 0; i < arr.count ; i++) {
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
smallScrollView.delegate = self;
img.image = [UIImage imageNamed:arr[i]];
[smallScrollView addSubview:img];
[album addSubview:smallScrollView];
smallScrollView.tag = i;
smallScrollView.minimumZoomScale = 0.5;
smallScrollView.maximumZoomScale = 2;
}
album.contentOffset = CGPointMake(WIDTH, 0);
album.bounces = NO;
album.showsHorizontalScrollIndicator = NO;
} - (void)addPage{
self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
self.page.currentPage = 0;
self.page.numberOfPages = 3;
[self.view addSubview:self.page];
} - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
_index = scrollView.tag;
return [scrollView.subviews firstObject];
} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
self.page.currentPage = index - 1;
if (index == 3) {
self.page.currentPage = 0;
scrollView.contentOffset = CGPointMake(0, 0);
}
if (index == 0) {
self.page.currentPage = 3;
scrollView.contentOffset = CGPointMake(WIDTH * 3, 0);
}
UIScrollView *scroll = [scrollView.subviews objectAtIndex:_index];
scroll.zoomScale = 1;
} - (void)pageAction:(UIPageControl *)page{
// 点击换图
[self.scrollViewOfAlbum setContentOffset:CGPointMake(WIDTH * (self.page.currentPage), 0) animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这就是我做的建议轮播图,比较简单,但是确实可以做到轮转效果。好了 祝大家有个愉快的周末。
网友评论