首页 技术 正文
技术 2022年11月12日
0 收藏 972 点赞 3,222 浏览 2641 个字

最后效果图:

BeyondViewController.h

//
// BeyondViewController.h
// 8_scrollVIew分页浏览
//
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@end

BeyondViewController.m

//
// BeyondViewController.m
// 8_scrollVIew分页浏览
/*
下面代码存在性能问题,仅作为新特性介绍界面使用
不可作为图片浏览器~
1,一次性生成8个ImageView会存在性能问题,解决方法:使用3个ImageView(或2个ImageView)
2,另外,循环播放还没实现
*/
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//#import "BeyondViewController.h"
// 图片总张数
#define kImgCount 8
@interface BeyondViewController ()<UIScrollViewDelegate>
{
// 分页条码指示控制器
UIPageControl *_pageControl;
}@end@implementation BeyondViewController- (void)viewDidLoad
{
[super viewDidLoad];
// 调用自己定义方法
[self scrollViewWithPage];
}// 带分页功能的scrollView
- (void)scrollViewWithPage
{ // 1,设置scrollView的可视大小,内容大小,等属性
_scrollView.frame = self.view.bounds;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.bouncesZoom = NO;
_scrollView.bounces = NO;
// 设置代码,监听滚动完成的事件
_scrollView.delegate = self; // 2,创建8个UIImageView,加入到scrollView
// 每一个图片宽,高
CGFloat imgW = self.view.bounds.size.width;
CGFloat imgH = self.view.bounds.size.height;
for (int i=0; i<kImgCount; i++) {
// UIImageView
// 图片名:01.jpg ~ 07.jpg
NSString *imgName = [NSString stringWithFormat:@"0%d.png",i+1];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
// 假设保持imageView里面的image不变形
//设置UIImageView的对象的下面两个属性,能够图片不变形且充满图片框为前提进行填充。
imgView.clipsToBounds = YES;
imgView.contentMode = UIViewContentModeScaleAspectFill;
// y是0,x是一张连着一张
imgView.frame = CGRectMake(i*imgW, 0, imgW, imgH);
// 将全部的图片加入到scrollView
[_scrollView addSubview:imgView];
} // 3,这个最重要,是滚动区域
// _scrollView.contentSize = CGSizeMake(kImgCount*imgW, imgH);
// 0代表高度方向不滚动
_scrollView.contentSize = CGSizeMake(kImgCount*imgW, 0);
// 按scrollView的宽度分页
_scrollView.pagingEnabled = YES; // 4,pageControl分页指示条
_pageControl = [[UIPageControl alloc]init];
// pageControl分页指示条的中心点在底部中间
_pageControl.numberOfPages = kImgCount; //这个最重要
_pageControl.center = CGPointMake(imgW*0.5, imgH-20);
_pageControl.bounds = CGRectMake(0, 0, 150, 15);
_pageControl.pageIndicatorTintColor = [UIColor grayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
_pageControl.enabled = NO; //取消其默认的点击行为
[self.view addSubview:_pageControl];}
/*
在这种方法里面,能够进行性能优化,由于时时在监听滚动,从而随时进行3个UIImageView的拼接,甚至可精简到仅仅有2个UIImageView进行动态拼接
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// scrollView的contentOffset是最重要的属性,点,x,y记录的是滚动的距离,相对的是scrollView的可视界面的左上角的距离
CGPoint offset = scrollView.contentOffset; int curPageNo = offset.x / _scrollView.bounds.size.width;
_pageControl.currentPage = curPageNo ;}@end

版权声明:本文博客原创文章,博客,未经同意,不得转载。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,474
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,889
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,724
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,479
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,118
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,278