看代码
#import "ViewController.h"
#define kNum 25
#define kAnimationTime 0.3
@interface ViewController ()<UIScrollViewDelegate>
{
UIView * _chooseView;
UIScrollView * _scrollView;
int num;
}
@property (nonatomic, strong)NSTimer * myTimer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatView];
/*加一个定时器,就可以自动切换视图,就类似平时的广告切换
dispatch_async(dispatch_get_main_queue(), ^{
num = 0;
_myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changView) userInfo:nil repeats:YES];
});
*/
// self.navigationController.navigationBar.translucent = NO;
}
- (void)creatView
{
//创建ScrollView
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNum, self.view.frame.size.width, self.view.frame.size.height-kNum)];
//设置内部空间
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height-kNum);
[self.view addSubview:_scrollView];
_scrollView.pagingEnabled = YES;//开启分页效果
_scrollView.delegate = self;
[_scrollView setShowsHorizontalScrollIndicator:NO];//关闭左右活动条
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-kNum);//每页大小
NSArray * buttonTitle = @[@"视图一",@"视图2"];
for (int i = 0; i < buttonTitle.count; i++) {
UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 * i, 18, self.view.frame.size.width/2, 15)];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:buttonTitle[i] forState:UIControlStateNormal];
button.tag = 100 + i;
[button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
for (int i = 0; i < buttonTitle.count; i++) {
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i,25, self.view.frame.size.width, self.view.frame.size.height-25)];
[_scrollView addSubview:view];
CGFloat hue = ( arc4random() % 256 / 256.0 );
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
view.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
_chooseView = [[UIView alloc]initWithFrame:CGRectMake(0, 45 , self.view.frame.size.width/2, 5)];
_chooseView.backgroundColor = [UIColor redColor];
[self.view addSubview:_chooseView];
}
- (void)changView
{
num++;
CGPoint page = CGPointMake(num * self.view.frame.size.width, 0);
[_scrollView setContentOffset:page animated:YES];
if (num == 1) {
num = -1;
}
}
#pragma mark - scrollView Deldgate
- (void) scrollViewDidScroll:(UIScrollView *)sender {
// 得到每页宽度
CGFloat pageWidth = sender.frame.size.width;
// 根据当前的x坐标和页宽度计算出当前页数
int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
CGFloat x =self.view.frame.size.width/2 * currentPage;
[UIView animateWithDuration:kAnimationTime animations:^{
_chooseView.frame = CGRectMake(x, 45, self.view.frame.size.width/2, 5);
}];
}
#pragma mark - button Aciton
- (void)chooseAction:(UIButton *)sender
{
CGFloat x =self.view.frame.size.width/2 * (sender.tag - 100);
[UIView animateWithDuration:kAnimationTime animations:^{
_chooseView.frame = CGRectMake(x, 45, self.view.frame.size.width/2, 5);
}];
//计算移动位置
CGPoint page = CGPointMake((sender.tag - 100)*self.view.frame.size.width, 0);
[_scrollView setContentOffset:page animated:YES];
}
点击按钮可切换视图,也可左右滑动切换视图。