iOS(四)动漫App:三
来源:互联网 发布:手机yy变声软件 编辑:程序博客网 时间:2024/04/30 09:32
这一章是关于HomepageViewController的,这一个ViewController花了很长时间才解决,主要涉及到了ScrollView,TableView,TableViewCell,Xml解析,以及对象之间的传值,服务器搭建花了一个月不到的时间才东拼西凑的整合到了HomepageViewController上,也是在这里我认为我用到了MVC框架
在这张图片上,有三个部分,顶上的ScrollView可以进行图片的滑动,中间的其实是三个按钮(人气最高动漫,漫画专区,动画专区(其实很后悔设计了三个按钮))最下面的tableViewCell(HomepageVierwController有tableView了,tableViewcell负责cell的样式),这三个部分就是我认为的View
那这些图片,视频(点击可以播放视频)从哪边来的呢,从我的阿里云服务器传过来的,当然并不是这么简单,先是通过特定的URL,链接到服务器,然后读出数据库的数据
数据库里存储动画的相关数据,名字,介绍,图片的URL,视频的URL,通过xml返回回来,这时候需要相对应的数据字典读取并存储
所以需要自己定义数据字典,我认为这就是model(数据),当然Controller控制器就是我们HomepageViewController,它负责数据的接收,视图的显示,当然我最想表达还是我们有一个隐藏部分,它负责网络数据的传送,也负责xml数据的解析!AFNetworking网络很多人都用这个框架
AFNetworking可以通过URL获取xml数据
#define URL @"http://ashuai.6655.la/HomepageView.php?data=1"
<pre name="code" class="php">
用php脚本对数据库进行搜索根据热门度select最热门的置顶,热门度低一点成为人气动画
并返回xml格式的数据
<?php include("xml/sql_dll.php");$dll=new sql_dll();if($_GET['data']){$top=$dll->select_comic_popular(2);foreach($top as $each_top_data){$return .="<top_image> <video_image> <value>$each_top_data[1]</value> </video_image> <video_url><value>$each_top_data[3]</value> </video_url> </top_image>"; }$comic_data=$dll->select_comic_popular(1); foreach($comic_data as $each_comic_data){ $return .="<comic> <video_name><value>$each_comic_data[0]</value></video_name><video_image><value>$each_comic_data[1]</value></video_image><video_intro><value>$each_comic_data[2]</value></video_intro><video_url><value>$each_comic_data[3]</value></video_url> </comic>"; } $animation_data=$dll->select_animation_popular(1); foreach($animation_data as $each_animation_data){ $return .="<animation> <video_name><value>$each_animation_data[0]</value></video_name><video_image><value>$each_animation_data[1]</value></video_image><video_intro><value>$each_animation_data[2]</value></video_intro><video_url><value>$each_animation_data[3]</value></video_url> </animation>"; } header("Content-type:text/xml"); echo '<?xml version="1.0" encoding="UTF-8"?><data>'.$return.'</data>';exit;}else{print("no data");}?>
-(void)xml_data{ NSString *url=URL; [AFNetworkTool xmlRequestWithUrl:url success:^(NSXMLParser *xmlParser) { self.xmldata=[NSMutableDictionary dictionary]; xmlParser.delegate = self; [xmlParser setShouldProcessNamespaces:YES]; [xmlParser parse]; } fail:^{ NSLog(@"xml请求失败"); UIAlertView *warn=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"网络链接失败!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [warn show]; }];}当php脚本返回xml格式的数据后,xml解析
#pragma mark - NSXMLParserDelegate-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if (qName) { self.elementName=qName; } if ([qName isEqualToString:@"top_image"]) { self.currentDictionary=[NSMutableDictionary dictionary]; } else if ([qName isEqualToString:@"comic"]) { self.currentDictionary=[NSMutableDictionary dictionary]; }else if ([qName isEqualToString:@"animation"]){ self.currentDictionary=[NSMutableDictionary dictionary]; } self.outstring=[NSMutableString string];}-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if (!self.elementName) { return; } [self.outstring appendFormat:@"%@",string];}-(void)parser:(NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{ if([qName isEqualToString:@"top_image"]){ NSMutableArray *array=[self.xmldata objectForKey:@"top_image"]; if (!array) { array =[NSMutableArray array]; } [array addObject:self.currentDictionary]; [self.xmldata setObject:array forKey:@"top_image"]; self.currentDictionary=nil; } else if([qName isEqualToString:@"comic"]){ NSMutableArray *array=[self.xmldata objectForKey:@"comic"]; if (!array) { array =[NSMutableArray array]; } [array addObject:self.currentDictionary]; [self.xmldata setObject:array forKey:@"comic"]; self.currentDictionary=nil; }else if ([qName isEqualToString:@"animation"]) { NSMutableArray *array=[self.xmldata objectForKey:@"animation"]; if (!array) { array =[NSMutableArray array]; } [array addObject:self.currentDictionary]; [self.xmldata setObject:array forKey:@"animation"]; self.currentDictionary=nil; } else if([qName isEqualToString:@"value"]) { } else if ([qName isEqualToString:@"video_name"]||[qName isEqualToString:@"video_url"]||[qName isEqualToString:@"video_intro"]||[qName isEqualToString:@"video_image"]){ [self.currentDictionary setObject:[NSArray arrayWithObject:[NSDictionary dictionaryWithObject:self.outstring forKey:@"value"]] forKey:qName]; } else { [self.currentDictionary setObject:self.outstring forKey:qName]; } self.elementName=nil;}-(void)parserDidEndDocument:(NSXMLParser *)parser{//xml解析结束 self.data=[NSDictionary dictionaryWithObject:self.xmldata forKey:@"data"];//数据存在data数据字典里 [self TableHeaderView];<span style="white-space:pre"></span>//scrollview以及button数据填充 [self.homePageTableView reloadData]; <span style="white-space:pre"></span> //HomepagetableViewcell的数据重新加载 }
解析的数据存在自己写的数据字典,我的xml最外面是一个<data>,里面有<top_image>,<comic>,<animation>三个数据包,<top_image>负责存储置顶动画(图片滑动的)的数据;<comic>负责存储人气动画的数据,<animation>负责存储人气漫画的数据
//// appnetdata_package.h// 动漫//// Created by LiuJiawei on 15/10/13.// Copyright © 2015年 LIUjiawei. All rights reserved.//#import <Foundation/Foundation.h>@interface NSDictionary (comic_package)-(NSArray *)top_image;-(NSArray *)comic;-(NSArray *)animation;@end
//// appnetdata_package.m// 动漫//// Created by LiuJiawei on 15/10/13.// Copyright © 2015年 LIUjiawei. All rights reserved.//#import "appnetdata_package.h"@implementation NSDictionary (comic_package)-(NSArray *)top_image{ NSDictionary *dict = [self objectForKey:@"data"]; return [dict objectForKey:@"top_image"];}-(NSArray *)animation{ NSDictionary *dict = [self objectForKey:@"data"]; return [dict objectForKey:@"animation"];}-(NSArray *)comic{ NSDictionary *dict = [self objectForKey:@"data"]; return [dict objectForKey:@"comic"];}@end
//// appnetdata.h// 动漫//// Created by LiuJiawei on 15/10/13.// Copyright © 2015年 LIUjiawei. All rights reserved.//#import <Foundation/Foundation.h>@interface NSDictionary (comic)-(NSString *)video_name;-(NSString *)video_image;-(NSString *)video_intro;-(NSString *)video_url;@end
#import "appnetdata.h"@implementation NSDictionary (comic)-(NSString *)video_name{ NSArray *ar = [self objectForKey:@"video_name"]; NSDictionary *dict = [ar objectAtIndex:0]; return [dict objectForKey:@"value"];}-(NSString *)video_image{ NSArray *ar = [self objectForKey:@"video_image"]; NSDictionary *dict = [ar objectAtIndex:0]; return [dict objectForKey:@"value"];}-(NSString *)video_intro{ NSArray *ar = [self objectForKey:@"video_intro"]; NSDictionary *dict = [ar objectAtIndex:0]; return [dict objectForKey:@"value"];}-(NSString *)video_url{ NSArray *ar = [self objectForKey:@"video_url"]; NSDictionary *dict = [ar objectAtIndex:0]; return [dict objectForKey:@"value"];}@end
数据解析和存储告一段落,接着是如何显示出来,显示出来主要由这两个负责
[self TableHeaderView];<span></span> //scrollview以及button数据填充 [self.homePageTableView reloadData]; <span></span> //HomepagetableViewcell的数据重新加载
我们看一下TableHeaderView的代码
-(void)TableHeaderView{ _headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 340)]; _headerView.backgroundColor=[UIColor clearColor]; _homeScrollView=[[HomeheadScroll alloc]initWithFrame:CGRectMake(0,0, kScreenWidth, 150)]; NSMutableArray *imageMutableArray=[NSMutableArray array];
//提取top_image的数据存放到head_data中,再存放到headview_data,接着根据key(video_image)读取图片的URL
NSDictionary *headerview_data; NSArray *head_data=[self.data top_image]; NSInteger head_count=[self.data top_image].count; for (int i=0; i<head_count; i++) { headerview_data=[head_data objectAtIndex:i]; NSString *image_url=[headerview_data video_image];
//根据URL返回image数据,传给我写的homeScrollView
UIImageView *image=[[UIImageView alloc]initWithFrame:_homeScrollView.bounds]; [AFNetworkTool imageRequestWithUrl:image_url success:^(UIImage *image_s) { image.image= image_s; } fail:^{ NSLog(@"图片加载失败"); }]; [imageMutableArray addObject:image]; } [_homeScrollView setImageViewAry:imageMutableArray]; [_headerView addSubview:_homeScrollView]; [_homeScrollView shouldAutoShow:YES]; _homeScrollView.delegate=self; _HomeheaderView=[[[NSBundle mainBundle]loadNibNamed:@"HomeheaderView" owner:self options:nil]lastObject]; _HomeheaderView.frame=CGRectMake(0, _homeScrollView.frame.size.height, kScreenWidth, 190); _HomeheaderView.delegate=self; [_headerView addSubview:_HomeheaderView]; NSLog(@"TableHeaderView"); _homePageTableView.tableHeaderView=_headerView; }存储image的数组赋值给homeScrollView的ImageViewAry,homeScrollView根据获得的image实现她的图片滑动
H<span style="font-family: Arial, Helvetica, sans-serif;">omeheadScroll.h</span>
#import <UIKit/UIKit.h>@protocol HomeheadScrollDelegate;@interface HomeheadScroll : UIView <UIScrollViewDelegate>{ __weak id <HomeheadScrollDelegate> _delegate;}@property(nonatomic,weak) id <HomeheadScrollDelegate> delegate;@property(nonatomic,assign)NSInteger currentPage; //当前页码@property(nonatomic,strong)NSMutableArray *imageViewAry; // 图片数组@property(nonatomic,readonly)UIScrollView *scrollView; @property(nonatomic,readonly)UIPageControl *pageControl; -(void)shouldAutoShow:(BOOL)shouldStart; //自动滑动@end@protocol HomeheadScrollDelegate <NSObject>@optional-(void)didClickPage:(HomeheadScroll *)view atIndex:(NSInteger)index; //图片点击事件@endHomeheadScroll.m
// HomeheadScroll.m////// Created by LiuJiawei on 15/9/24.// Copyright © 2015年 LIUjiawei. All rights reserved.//#import "HomeheadScroll.h"#define kZSGBgColor [UIColor colorWithRed:254.f/255.f green:87.f/255.f blue:178.f/255.f alpha:1]#define viewWidth self.bounds.size.width#define viewHeight self.bounds.size.height@interface HomeheadScroll(){ UIView *firstView;//前一页view UIView *middleView;//当前显示的view UIView *lastView;//后一页的view NSTimer *autoScrollTimer; UITapGestureRecognizer *tap;}@end@implementation HomeheadScroll-(id)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { //设置scrollview _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)]; _scrollView.delegate=self; _scrollView.contentSize=CGSizeMake(viewWidth*3, viewHeight); _scrollView.showsHorizontalScrollIndicator=NO; _scrollView.pagingEnabled=YES; _scrollView.backgroundColor=[UIColor blackColor]; _scrollView.delegate=self; [self addSubview:_scrollView]; //设置分页 _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, viewHeight-30, viewWidth, 30)]; _pageControl.userInteractionEnabled=NO; _pageControl.currentPageIndicatorTintColor= kZSGBgColor; _pageControl.pageIndicatorTintColor=[UIColor whiteColor]; [self addSubview:_pageControl]; //设置单机手势,然后进入当前页面的子页! tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; tap.numberOfTapsRequired=1; tap.numberOfTouchesRequired=1; [_scrollView addGestureRecognizer:tap]; } return self;}-(void)handleTap:(UITapGestureRecognizer *)sender{ if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) { [_delegate didClickPage:self atIndex:_currentPage]; }}#pragma mark 设置imageViewAry-(void)setImageViewAry:(NSMutableArray *)imageViewAry{ if (imageViewAry) { _imageViewAry=imageViewAry; _currentPage=0; _pageControl.numberOfPages=_imageViewAry.count; } [self reloadData];}#pragma mark刷新view-(void)reloadData{ [firstView removeFromSuperview]; [middleView removeFromSuperview]; [lastView removeFromSuperview]; //从数组中取到对应的图片view加到已定义的三个view中 if (_currentPage==0) { firstView = [_imageViewAry lastObject]; middleView = [_imageViewAry objectAtIndex:_currentPage]; lastView = [_imageViewAry objectAtIndex:_currentPage+1]; } else if (_currentPage == _imageViewAry.count-1) { firstView = [_imageViewAry objectAtIndex:_currentPage-1]; middleView = [_imageViewAry objectAtIndex:_currentPage]; lastView = [_imageViewAry firstObject]; } else { firstView = [_imageViewAry objectAtIndex:_currentPage-1]; middleView = [_imageViewAry objectAtIndex:_currentPage]; lastView = [_imageViewAry objectAtIndex:_currentPage+1]; } //设置三个view的frame,加到scrollview上 firstView.frame = CGRectMake(0, 0, viewWidth, viewHeight); middleView.frame = CGRectMake(viewWidth, 0, viewWidth, viewHeight); lastView.frame = CGRectMake(viewWidth*2, 0, viewWidth, viewHeight); [_scrollView addSubview:firstView]; [_scrollView addSubview:middleView]; [_scrollView addSubview:lastView]; //设置当前的分页 _pageControl.currentPage = _currentPage; //显示中间页 _scrollView.contentOffset = CGPointMake(viewWidth, 0);}#pragma mark scrollvie停止滑动-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ //手动滑动时候暂停自动替换 [autoScrollTimer invalidate]; autoScrollTimer = nil; autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES]; //得到当前页数 float x = _scrollView.contentOffset.x; //往前翻 if (x<=0) { if (_currentPage-1<0) {// _currentPage = _imageViewAry.count-1; _currentPage=3; return ; }else{ _currentPage --; } } //往后翻 if (x>=viewWidth*2) { if (_currentPage==_imageViewAry.count-1) { _currentPage = 0; }else{ _currentPage ++; } } [self reloadData];}#pragma mark 自动滚动-(void)shouldAutoShow:(BOOL)shouldStart{ if (shouldStart) //开启自动翻页 { if (!autoScrollTimer) { autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES]; } } else //关闭自动翻页 { if (autoScrollTimer.isValid) { [autoScrollTimer invalidate]; autoScrollTimer = nil; } }}#pragma mark 展示下一页-(void)autoShowNextImage{ if (_currentPage == _imageViewAry.count-1) { _currentPage = 0; }else{ _currentPage ++; } [self reloadData];}@end
主页上的图片ScrollView讲完了,
下一章讲中间那三个按钮(其实并不需要讲这个,这个很简单),和tableViewcell
http://blog.csdn.net/u012723810/article/details/50489259
- iOS(四)动漫App:三
- iOS(四)动漫App:四
- iOS(四)动漫App:一
- iOS(四)动漫App:二
- iOS(四)动漫App:五
- iOS(四)动漫App:六
- 红旅动漫App
- 动漫
- 《快乐星猫》四格搞笑动漫```
- 动漫推荐之四叶游戏
- ios app安装的四种方式
- iOS APP安全杂谈之三
- iOS APP 逆向安全杂谈之三
- iOS 给测试人员测试手机APP的四种方法:真机运行(略),打ipa包,(testFlighe)邮件,蒲公英(三)蒲公英
- ios app推送通知详细教程 四 实战问题解决
- iOS打包ipa的四种方法(.app转.ipa)
- iOS游戏开发三:苹果内支付实现 In-app
- iOS安全攻防(三):使用Reveal分析他人app
- cocos2d-x 3.4 vs+cocostudio类神经猫三消游戏《Rabbit Escape》
- 在iOS地图上绘制两点间路线
- CSS学习笔记——盒模型,块级元素和行内元素的区别和区别
- Android使用Ant批量打包应用步骤
- FastJson
- iOS(四)动漫App:三
- linux 上传下载测速
- graphx操作实例05-VertexRDD和EdgeRDD属性测试
- .net 解析xml
- 管理时间做更好的自己
- 汽车之家 报价
- 使用滑动窗口进行人脸检测 Face detection with a sliding window
- 浅谈 PostgreSQL 的 timestamp 类型
- UVa227 and acm/icpc world finals 1993 谜题