UIScrollview的使用方法:

1.今天学习了一下UIScrollveiw的使用方法:就是在欢迎界面的使用来介绍产品的新特性的代码:

下面有两种方法来实现其中代码:(不多说废话,直接来代码)使用xib实现的

#import "WecomeViewController1.h"

#import "AppDelegate.h"

#import "QuestionViewController.h"

@interface WecomeViewController1 ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@property (weak, nonatomic) IBOutlet UIPageControl *pagecount;

@end

@implementation WecomeViewController1

- (void)viewDidLoad {

[super viewDidLoad];

[self setUpscrollview];

[self setUppageview];

}

-(void)setUpscrollview{

self.scrollview.delegate = self; //设置代理

//设置图片

UIImageView *imageview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover1.png"]];

UIImageView *imageview2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover2.png"]];

UIImageView *imageview3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover3.png"]];

//设置frmae

imageview1.frame = CGRectMake(0, 0, screenWitdth, screenHeight);

imageview2.frame = CGRectMake(screenWitdth, 0, screenWitdth, screenHeight);

imageview3.frame = CGRectMake(2*screenWitdth, 0, screenWitdth, screenHeight);

//填充模式

imageview1.contentMode = UIViewContentModeScaleAspectFit;

imageview2.contentMode = UIViewContentModeScaleAspectFit;

imageview3.contentMode = UIViewContentModeScaleAspectFit;

//在第三张图片上面加一个button

imageview3.userInteractionEnabled = YES;

UIButton *customButton = [[UIButton alloc]init];

[customButton setBackgroundImage:[UIImage imageNamed:@"start1.png"] forState:UIControlStateNormal];

CGFloat centerX = imageview3.frame.size.width * 0.5;

CGFloat centerY = imageview3.frame.size.height * 0.9;

customButton.center = CGPointMake(centerX, centerY);

customButton.bounds = (CGRect){CGPointZero, 100 ,40};

[customButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

[imageview3 addSubview:customButton];

//将图添加到scrllview上面去

[self.scrollview addSubview:imageview1];

[self.scrollview addSubview:imageview2];

[self.scrollview addSubview:imageview3];

//设置scrollvew的一些属性

self.scrollview.contentSize = CGSizeMake(3*screenWitdth, 0);

self.scrollview.pagingEnabled = YES;

self.scrollview.showsHorizontalScrollIndicator = NO;

self.scrollview.showsVerticalScrollIndicator = NO;

self.scrollview.bounces = NO;

}

-(void)login{

NSLog(@"登陆界面");

QuestionViewController *questVC = [[QuestionViewController alloc]init];

[self presentViewController:questVC animated:YES completion:nil];

}

//创建pagecontoller

-(void)setUppageview{

self.pagecount.numberOfPages = 3;

self.pagecount.currentPageIndicatorTintColor = [UIColor grayColor];

self.pagecount.pageIndicatorTintColor = [UIColor blackColor];

}

#pragma mark -UIScrollDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

//计算水平的距离

CGFloat offsetX = self.scrollview.contentOffset.x;

//求页码

double pageDouble = offsetX / scrollView.frame.size.width;

int pageInt = (int)(pageDouble + 0.5);

self.pagecount.currentPage = pageInt;

}

@end

第二种纯代码实现:

#define CWNewfeatureImageCount 3

#import "CWNewfeatureViewController.h"

#import "CWTabBarViewController.h"

@interface CWNewfeatureViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation CWNewfeatureViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// 1.添加UISrollView

[self setupScrollView];

// 2.添加pageControl

[self setupPageControl];

}

/**

*  添加pageControl

*/

- (void)setupPageControl

{

// 1.添加

UIPageControl *pageControl = [[UIPageControl alloc] init];

pageControl.numberOfPages = CWNewfeatureImageCount;

CGFloat centerX = self.view.frame.size.width * 0.5;

CGFloat centerY = self.view.frame.size.height - 30;

pageControl.center = CGPointMake(centerX, centerY);

pageControl.bounds = CGRectMake(0, 0, 100, 30);

pageControl.userInteractionEnabled = NO;

[self.view addSubview:pageControl];

self.pageControl = pageControl;

// 2.设置圆点的颜色

pageControl.currentPageIndicatorTintColor = CWColor(253, 98, 42);

pageControl.pageIndicatorTintColor = CWColor(189, 189, 189);

}

-(void)setupScrollView

{

UIScrollView *scrollView = [[UIScrollView alloc] init];

scrollView.frame = self.view.bounds;

scrollView.delegate = self;

[self.view addSubview:scrollView];

// 2.添加图片

CGFloat imageW = scrollView.frame.size.width;

CGFloat imageH = scrollView.frame.size.height;

for (int index = 0; index<CWNewfeatureImageCount; index++) {

UIImageView *imageView = [[UIImageView alloc] init];

// 设置图片

NSString *name = nil;

if (fourInch) {

name = [NSString stringWithFormat:@"new_feature_%d-568h", index + 1];

} else {

name = [NSString stringWithFormat:@"new_feature_%d", index + 1];

}

imageView.image = [UIImage imageWithName:name];

// 设置frame

CGFloat imageX = index * imageW;

imageView.frame = CGRectMake(imageX, 0, imageW, imageH);

[scrollView addSubview:imageView];

// 在最后一个图片上面添加按钮

if (index == CWNewfeatureImageCount - 1) {

[self setupLastImageView:imageView];

}

}

// 3.设置滚动的内容尺寸

scrollView.contentSize = CGSizeMake(imageW * CWNewfeatureImageCount, 0);

scrollView.showsHorizontalScrollIndicator = NO;

scrollView.pagingEnabled = YES;

scrollView.bounces = NO;

}

/**

*  添加内容到最后一个图片

*/

- (void)setupLastImageView:(UIImageView *)imageView

{

// 0.让imageView能跟用户交互

imageView.userInteractionEnabled = YES;

// 1.添加开始按钮

UIButton *startButton = [[UIButton alloc] init];

[startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];

[startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];

// 2.设置frame

CGFloat centerX = imageView.frame.size.width * 0.5;

CGFloat centerY = imageView.frame.size.height * 0.6;

startButton.center = CGPointMake(centerX, centerY);

startButton.bounds = (CGRect){CGPointZero, startButton.currentBackgroundImage.size};

// 3.设置文字

[startButton setTitle:@"开始微博" forState:UIControlStateNormal];

[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];

[imageView addSubview:startButton];

// 4.添加checkbox

UIButton *checkbox = [[UIButton alloc] init];

checkbox.selected = YES;

[checkbox setTitle:@"分享给大家" forState:UIControlStateNormal];

[checkbox setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];

[checkbox setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];

checkbox.bounds = CGRectMake(0, 0, 200, 50);

CGFloat checkboxCenterX = centerX;

CGFloat checkboxCenterY = imageView.frame.size.height * 0.5;

checkbox.center = CGPointMake(checkboxCenterX, checkboxCenterY);

[checkbox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

checkbox.titleLabel.font = [UIFont systemFontOfSize:15];

[checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];

checkbox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);

[imageView addSubview:checkbox];

}

- (void)checkboxClick:(UIButton *)checkbox

{

checkbox.selected = !checkbox.isSelected;

}

/**

*  开始微博

*/

- (void)start

{

// 显示状态栏

[UIApplication sharedApplication].statusBarHidden = NO;

// 切换窗口的根控制器

self.view.window.rootViewController = [[CWTabBarViewController alloc] init];

}

/**

*  只要UIScrollView滚动了,就会调用

*

*/

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

// 1.取出水平方向上滚动的距离

CGFloat offsetX = scrollView.contentOffset.x;

// 2.求出页码

double pageDouble = offsetX / scrollView.frame.size.width;

int pageInt = (int)(pageDouble + 0.5);

self.pageControl.currentPage = pageInt;

}

@end

效果图:

时间: 2024-09-28 15:58:15

UIScrollview的使用方法:的相关文章

UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置

一个UICollectionView有好多个cell,滑动一下,谁也不知道会停留在哪个cell,滑的快一点,就会多滑一段距离,反之则会滑的比较近,这正是UIScrollview用户体验好的地方. 如果想要UICollectionView停留到某个cell的位置,可以用 - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPos

关于UIScrollView属性跟方法的总结

iOS中UIScollView的总结 在iOS开发中可以说UIScollView是所有滑动类视图的基础,包括UITableView,UIWebView,UICollectionView等等,UIScrollView类为显示大于应用程序窗口的内容提供支持.它使得用户可以使用滑动手势来滚动,并可以使用扩张/收缩手势来放大缩小部分内容.UIScrollView类可以拥有一个代理,该代理采用UIScrollViewDelegate协议.对于缩放的工作,代理必须实现viewForZoomingInScro

UIScrollView 的代理方法简单注解

//减速停止了时执行,手触摸时执行执行 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; //只要滚动了就会触发 - (void)scrollViewDidScroll:(UIScrollView *)scrollView; //开始拖拽视图 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; //完成拖拽 - (void)scrollVie

iOS判断UIScrollView的滚动方法

- (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = scrollView.contentOffset.y; if (newY != _oldY) { if (newY > _oldY && (newY - _oldY) > 100) { NSLog(@"Down"); _oldY = newY; } else if (newY < _oldY &&am

UIScrollView

1. UIScrollView的创建和常用的属性 1> 概述 UIScrollView 是 UIView 的子类, 所以我们可以仿照 UIView 的创建步骤创建一个 UIScrollView UIScrollView 作为所有的滚动视图的基类, 所有学好 UIScrollView 也成为学好 UITableView 和 UICollectionView等滚动视图的前提 UIScrollView 主要使用在滚动头条(轮播图),相册等常见的功能里 2> 常用的属性 1     // 创建对象 2

UIScrollView —— 基本使用(一)

1 是什么: UIScrollView是一个能够滚动的视图控件,可以用来展示大量的内容,并且可以通过滚动查看所有的内容 2 怎么用: UIScrollView的基本使用 1> UIScrollView的用法很简单 1.1 将需要展示的内容添加到UIScrollView中 1.2 设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉它滚动的范围(能滚多远,滚到哪里是尽头) 1.3 UIScrollView显示内容的小细节 超出UIScro

UIScrollView常用属性使用

一 UIScrollView 的基本属性 UIScrollView *scrollView = [[UIScrollView alloc] init];//创建UIScrollView scrollView.frame = CGRectMake(0, 0, 250, 250); // frame中的size指UIScrollView的可视范围 指定UIScrollView可滑动的距离大小 scrollView.backgroundColor = [UIColor grayColor];//指定U

UIScrollView 图片分页显示,这里用到了UIPageControl

#import "MJViewController.h" #define kCount 8  //定义宏,程序中经常使用,方便扩展 @interface MJViewController () <UIScrollViewDelegate> { UIPageControl *_pageControl; } @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; CGFlo

UIScrollView 实践经验

转载自:http://tech.glowing.com/cn/practice-in-uiscrollview/ UIScrollView(包括它的子类 UITableView 和 UICollectionView)是 iOS 开发中最常用也是最有意思的 UI 组件,大部分 App 的核心界面都是基于三者之一或三者的组合实现.UIScrollView 是 UIKit 中为数不多能响应滑动手势的 view,相比自己用 UIPanGestureRecognizer 实现一些基于滑动手势的效果,用 U