你的位置:首页 > 软件开发 > 操作系统 > 利用UIScrollView实现几个页面的切换

利用UIScrollView实现几个页面的切换

发布时间:2015-09-14 23:00:08
此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容;原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514效果图如下: 1:知识点滚动视图的运用#import "YCView.h&quo ...

利用UIScrollView实现几个页面的切换

此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容;

原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514

效果图如下:

利用UIScrollView实现几个页面的切换

 

1:知识点滚动视图的运用

#import "YCView.h"@interface ViewController ()<UIScrollViewDelegate>@property (nonatomic, strong)UIScrollView *scrollV;@property (weak, nonatomic) IBOutlet UIButton *usesbtn;@property (weak, nonatomic) IBOutlet UIButton *partBtn;@property (weak, nonatomic) IBOutlet UIButton *serverBtn;@end@implementation ViewController//懒加载- (UIScrollView *)scrollV{  if(!_scrollV)  {    _scrollV = [[UIScrollView alloc] init];    //设置scrollView的frame    CGFloat scrollX = 0;    CGFloat scrollY = 110;    CGFloat scrollW = CGRectGetWidth(self.view.bounds);    CGFloat scrollH = CGRectGetHeight(self.view.bounds);    _scrollV.frame = CGRectMake(scrollX, scrollY, scrollW, scrollH);    //设置代理    _scrollV.delegate = self;    //将scrollView添加到控制器的view上    [self.view addSubview:_scrollV];      }  return _scrollV;}- (void)viewDidLoad {  [super viewDidLoad];  //添加视图 view  [self addScrollView];  self.scrollV.contentOffset = CGPointMake(0, 0);}- (void)addScrollView{  //添加3个view  for(int i = 0; i < 3; i++)  {    CGFloat viewX = i * [UIScreen mainScreen].bounds.size.width;    CGFloat viewY = 0;    CGFloat viewW = [UIScreen mainScreen].bounds.size.width;    CGFloat viewH = [UIScreen mainScreen].bounds.size.height - 108;    YCView *v = [[YCView alloc] initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];    v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0];    [self.scrollV addSubview:v];  }  //设置frame,偏移量  //设置分页  self.scrollV.pagingEnabled = YES;  self.scrollV.backgroundColor = [UIColor orangeColor];  //设置滚动范围  self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, [UIScreen mainScreen].bounds.size.height);  //设置偏移量  self.scrollV.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);  //取消scrollView滚动到边缘的弹簧效果  self.scrollV.bounces = NO;  //隐藏水平滚动条  self.scrollV.showsHorizontalScrollIndicator = NO;}#pragma mark --UIScrollViewDelegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView{  //设置按钮被选中状态下的颜色  scrollView.contentOffset.x == 0 ? [self.usesbtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.usesbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) ? [self.partBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.partBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) * 2 ? [self.serverBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.serverBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];}#pragma mark --btnClick- (IBAction)usesBtnClick:(id)sender {  //跳转到第1个view contentOffset.x = 屏幕的宽度 * 0  //重置scrollView的位置  [UIView animateWithDuration:0.5 animations:^{    self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:0];  }];}- (IBAction)partBtnClick:(id)sender {  //跳转到第2个view contentOffset.x = 屏幕的宽度 * 1  //重置scrollView的位置  [UIView animateWithDuration:0.5 animations:^{    self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:1];  }];  }- (IBAction)serverBtnClick:(id)sender {  //跳转到第3个view contentOffset.x = 屏幕的宽度 * 2  //重置scrollView的位置  [UIView animateWithDuration:0.5 animations:^{    self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:2];  }];}//返回scrollView偏移量- (CGPoint)ScrollViewWithContentOffSetPage:(NSInteger)page{  return CGPointMake(([UIScreen mainScreen].bounds.size.width) * page, 0);}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}@end

原标题:利用UIScrollView实现几个页面的切换

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。