当前位置: 首页 > news >正文

Slideshow ad

网页上面常用的Jquery广告控件(slideshow)是通过div+jquery来实现,ios上面实现主要通过UIPageControl+UIScrollView来结合实现,效果如下图:

源码如下:

1 #import <Foundation/Foundation.h>
2 @class SlideView;
3
4 @protocol SlideScrollDelegate <NSObject>
5
6 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage;
7
8 @end
复制代码
 1 #import <UIKit/UIKit.h>
2 #import "SlideView.h"
3 #import "SlideScrollDelegate.h"
4
5 @interface SlideViewController : UIViewController<SlideScrollDelegate>
6 {
7 NSMutableArray *pages;
8 SlideView *slideshow;
9 }
10
11 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage;
12
13 @end
复制代码
 1 #import "SlideViewController.h"
2 static const int PAGESIZE=5;
3
4 @implementation SlideViewController
5
6 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
7 {
8 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
9 if (self) {
10 // Custom initialization
11 }
12 return self;
13 }
14
15 - (void)didReceiveMemoryWarning
16 {
17 // Releases the view if it doesn't have a superview.
18 [super didReceiveMemoryWarning];
19
20 // Release any cached data, images, etc that aren't in use.
21 }
22
23 #pragma mark - View lifecycle
24
25
26 // Implement loadView to create a view hierarchy programmatically, without using a nib.
27 - (void)loadView
28 {
29 [super loadView];
30
31 pages=[[NSMutableArray alloc]init];
32 for (int i=0; i<PAGESIZE; i++) {
33 UIImage *background=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.yishimai.com/ios/slideshow/images/black.png"]]];
34
35 UIImage *image=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yishimai.com/ios/slideshow/images/%d.jpg",i+1]]]];
36
37 UIImageView *page=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
38 page.image=background;
39
40 UIImageView *subview=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
41 subview.image=image;
42 subview.bounds=CGRectMake(0.0, 0.0, image.size.width, image.size.height);
43
44 [page addSubview:subview];
45 [pages addObject:page];
46 }
47
48 slideshow=[[SlideView alloc]initWithFrame:self.view.frame];
49 slideshow.pages = pages;
50 slideshow.delegate = self;
51 self.view = slideshow;
52 }
53
54 -(void)dealloc
55 {
56 [slideshow release];
57 [super dealloc];
58 }
59
60
61
62 - (void)viewDidUnload
63 {
64 [super viewDidUnload];
65 }
66
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
68 {
69
70 return (interfaceOrientation == UIInterfaceOrientationPortrait);
71 }
72
73
74 -(void)slideshowDidChangeCurrentView:(SlideView *)slideview CurrentPage:(int) currentPage
75 {
76 NSLog(@"当前第 %d 页\n",currentPage);
77 }
78
79 @end
复制代码
 1 #import <UIKit/UIKit.h>
2 #import "SlideScrollDelegate.h"
3
4 @interface SlideView : UIView<UIScrollViewDelegate>
5 {
6 UIScrollView *scrollview;
7 UIPageControl *pageControl;
8
9 CGRect _pageRegion,_controlRegion;
10 NSMutableArray *_pages;
11 id _delegate;
12 }
13
14 -(void)layoutViews;
15 -(void)notifyPageChange;
16
17 @property(nonatomic,assign,getter = getPages) NSMutableArray *pages;
18 @property(nonatomic,assign,getter = getCurrentPage)int currentPage;
19 @property(nonatomic,assign,getter = getDelegate)id delegate;
20
21
22 @end
复制代码
  1 #import "SlideView.h"
2
3 @implementation SlideView
4
5 - (id)initWithFrame:(CGRect)frame
6 {
7 self = [super initWithFrame:frame];
8 if (self) {
9 _pages=nil;
10 _pageRegion=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height-50);
11 _controlRegion=CGRectMake(frame.origin.x, frame.size.height-frame.size.height/2+50, frame.size.width,10);
12 self.delegate=nil;
13
14 scrollview=[[UIScrollView alloc]initWithFrame:_pageRegion];
15 scrollview.pagingEnabled=YES;
16 scrollview.showsHorizontalScrollIndicator=NO;
17 scrollview.delegate=self;
18
19 [self addSubview:scrollview];
20
21 pageControl=[[UIPageControl alloc]initWithFrame:_controlRegion];
22 [pageControl addTarget:self action:@selector(pageControlDidChange:) forControlEvents:UIControlEventValueChanged];
23 [self addSubview:pageControl];
24 }
25 return self;
26 }
27
28 //设置内页
29 -(void)setPages:(NSMutableArray *)pages
30 {
31 if(_pages!=nil)
32 {
33 for (int i=0; i<[_pages count]; i++) {
34 [[_pages objectAtIndex:i]removeFromSuperview];
35 }
36 }
37
38 _pages=pages;
39 scrollview.contentOffset=CGPointMake(0.0, 0.0);
40 scrollview.contentSize=CGSizeMake(_pageRegion.size.width*[_pages count], _pageRegion.size.height);
41 pageControl.numberOfPages=[_pages count];
42 pageControl.currentPage=0;
43 [self layoutViews];
44
45 }
46
47 //拖动结束改变pageView上面的点位置
48 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
49 {
50 self.currentPage=self.currentPage;
51 [self notifyPageChange];
52 }
53
54
55 -(NSMutableArray *)getPages
56 {
57 return _pages;
58 }
59
60 //加载Scrollview内容
61 -(void) layoutViews
62 {
63 for (int i=0; i<[_pages count]; i++) {
64 UIView *page=[_pages objectAtIndex:i];
65 CGRect bounds=page.bounds;
66 CGRect frame=CGRectMake(_pageRegion.size.width*i, 0.0, _pageRegion.size.width, _pageRegion.size.height);
67 page.frame = frame;
68 page.bounds=bounds;
69 [scrollview addSubview:page];
70 }
71 }
72
73 -(id)getDelegate
74 {
75 return _delegate;
76 }
77
78 -(void)setDelegate:(id)delegate
79 {
80 _delegate=delegate;
81 }
82 //设置当前页
83 -(void)setCurrentPage:(int)currentPage
84 {
85 [scrollview setContentOffset:CGPointMake(_pageRegion.size.width*currentPage, scrollview.contentOffset.y) animated:YES];
86 pageControl.currentPage=currentPage;
87 }
88 //得到当前页
89 -(int)getCurrentPage
90 {
91 return (int)(scrollview.contentOffset.x/_pageRegion.size.width);
92 }
93 //点击pageview上面的点
94 -(void)pageControlDidChange:(id) sender
95 {
96 UIPageControl *control=(UIPageControl *)sender;
97 if(control==pageControl)
98 {
99 self.currentPage=control.currentPage;
100 }
101 [self notifyPageChange];
102 }
103 //通知委托事件
104 -(void)notifyPageChange
105 {
106 if(self.delegate!=nil)
107 {
108 if([_delegate conformsToProtocol:@protocol(SlideScrollDelegate)])
109 {
110 if ([_delegate respondsToSelector:@selector(slideshowDidChangeCurrentView:CurrentPage:)]) {
111 [self.delegate slideshowDidChangeCurrentView: (SlideView *)self CurrentPage:self.currentPage];
112 }
113 }
114 }
115 }
116
117
118
119 @end
复制代码
1 #import <UIKit/UIKit.h>
2 #import "SlideViewController.h"
3
4 @interface AppDelegate : UIResponder <UIApplicationDelegate>
5
6 @property (strong, nonatomic) UIWindow *window;
7 @property (strong, nonatomic) SlideViewController *viewController;
8
9 @end
复制代码
 1 #import "AppDelegate.h"
2
3 @implementation AppDelegate
4
5 @synthesize window = _window;
6 @synthesize viewController=_viewController;
7
8 - (void)dealloc
9 {
10 [_viewController release];
11 [_window release];
12 [super dealloc];
13 }
14
15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
16 {
17 CGRect screenBounds=[[UIScreen mainScreen] bounds];
18 // CGRect windowBounds=CGRectMake(0, 0, 320, 150);
19 //windowBounds.origin.y=0;
20 self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
21 self.viewController=[[[SlideViewController alloc]init]autorelease];
22 self.window.backgroundColor = [UIColor blackColor];
23
24 [self.window addSubview:self.viewController.view];
25 [self.window makeKeyAndVisible];
26 return YES;
27 }
复制代码



相关文章:

  • 13.6.1 新添加一个界面(Adding One Interface)
  • asp.net 机试题1
  • 第二节 1面向对像简介
  • basic4android 开发教程翻译(八)使用ListView
  • 虚拟机安装Linux怎么使桌面铺满虚拟机
  • xml.exist() 实例演示
  • CentOS 5.5 使用 EPEL 和 RPMForge 软件库
  • IMSI 小记
  • 1.7亿次接力 百度知道成雷锋精神新载体
  • 知识管理(KM) 企业文化的最好“沉淀”
  • cacti监控squid
  • 在Red Hat 4 AS U7上安装oracle10gR2
  • ctrl+shift+c
  • EFCodeFirst系列
  • 深入理解JavaScript系列(4):立即调用的函数表达式
  • java2019面试题北京
  • Java方法详解
  • log4j2输出到kafka
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 后端_MYSQL
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 前嗅ForeSpider中数据浏览界面介绍
  • 微信小程序实战练习(仿五洲到家微信版)
  • 为视图添加丝滑的水波纹
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 转载:[译] 内容加速黑科技趣谈
  • ${factoryList }后面有空格不影响
  • $NOIp2018$劝退记
  • (06)Hive——正则表达式
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (分布式缓存)Redis哨兵
  • (篇九)MySQL常用内置函数
  • (四)鸿鹄云架构一服务注册中心
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)重识new
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .mysql secret在哪_MySQL如何使用索引
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET 回调、接口回调、 委托
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • /3GB和/USERVA开关
  • @Autowired多个相同类型bean装配问题
  • @拔赤:Web前端开发十日谈
  • [Android] Upload package to device fails #2720
  • [ASP.NET MVC]Ajax与CustomErrors的尴尬
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [COGS 622] [NOIP2011] 玛雅游戏 模拟
  • [ComfyUI进阶教程] animatediff视频提示词书写要点
  • [LeetCode]--61. Rotate List
  • [MQ]常用的mq产品图形管理web界面或客户端
  • [MySQL FAQ]系列 -- 如何利用触发器实现账户权限审计