UI基础知识
scrollerview:
无限循环#import "ViewController.h"
@interface ViewController () <UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
self.view.backgroundColor = [UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 20)];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"顺序是粉,黄,青";
[self.view addSubview:label];
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 60, 300, 100)];
NSMutableArray *imageNames = [NSMutableArray arrayWithObjects:@"CyanSquare", @"MagentaSquare", @"YellowSquare", @"CyanSquare", @"MagentaSquare", nil];
for (int i = 0; i<imageNames.count; i++) {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(i*300, 0, 300, 100)];
iv.image = [UIImage imageNamed:imageNames[i]];
[sv addSubview:iv];
}
sv.delegate = self;
sv.showsHorizontalScrollIndicator = NO;
sv.pagingEnabled = YES;
sv.contentSize = CGSizeMake(imageNames.count*sv.frame.size.width, sv.frame.size.height);
sv.contentOffset = CGPointMake(sv.frame.size.width, 0);
[self.view addSubview:sv];
UIPageControl *pc = [[UIPageControl alloc] initWithFrame:CGRectMake(10, 200, 300, 10)];
pc.numberOfPages = 3;
pc.tag = 1;
[self.view addSubview:pc];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pc = (UIPageControl *)[self.view viewWithTag:1];
CGFloat xMove = scrollView.contentOffset.x;
if (xMove == 0) {
scrollView.contentOffset = CGPointMake(300*3, 0);
pc.currentPage = 2;
} else if (xMove == 300*4) {
scrollView.contentOffset = CGPointMake(300, 0);
pc.currentPage = 0;
} else {
pc.currentPage = xMove/300-1;
}
}
滚动选中
#import "NewsViewController.h"
#import "AddViewController.h"
#import "Model1.h"
#import "UIImageView+WebCache.h"
#import "NewsCell.h"
#import "AdCell.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "HdpicDetailViewController.h"
#import "ViewController.h"
#import "CommentViewController.h"
#import "Comment2ViewController.h"
#define winWidth [[UIScreen mainScreen] bounds].size.width
#define winHeight [[UIScreen mainScreen] bounds].size.height
#define headHeight 200
@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
NSMutableArray *_cmsArray;
NSMutableArray *_hdpicArray;
NSMutableArray *_SVCon;
UITableView *_tableView;
UIScrollView *_bigSV;
UIScrollView *_sv;
UIScrollView *_scroView;
UIPageControl *pc;
int page;
int num;
UIButton *_button;
}
@end
@implementation NewsViewController
- (void)btnclick:(UIButton *)sender{
for (int i=0; i<8; i++) {
UIButton *btn0 = (UIButton *)[_sv viewWithTag:1000+i];
[btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
page = sender.tag -1000;
[_cmsArray removeAllObjects];
[_hdpicArray removeAllObjects];
[self CreatTableView];
for (int i=0; i<=page; i++) {
_bigSV.contentOffset = CGPointMake((sender.tag-1000)*winWidth, 0);
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
_cmsArray = [[NSMutableArray alloc] init];
_hdpicArray = [[NSMutableArray alloc] init];
_SVCon = [[NSMutableArray alloc] init];
page = 0;
num = 0;
_bigSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, winWidth, winHeight-113)];
_bigSV.pagingEnabled = YES;
_bigSV.delegate = self;
_bigSV.tag = 50;
_bigSV.bounces = YES;
_bigSV.showsHorizontalScrollIndicator = NO;
_bigSV.contentSize = CGSizeMake(8*winWidth, winHeight-113);
[self CreatTableView];
}
//右上角按钮点击事件
- (void)rightClick:(UIButton *)sender{
AddViewController *add = [[AddViewController alloc] init];
[_sv removeFromSuperview];
add.title = @"更多";
[self.navigationController pushViewController:add animated:YES];
}
#pragma mark - 导航上面的scrollview
- (void)smallSV{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"slideTab_rightButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightClick:)];
_sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, winWidth-50, 44)];
_sv.contentSize = CGSizeMake((winWidth-50)*2, 44);
_sv.delegate = self;
_sv.bounces = NO;
_sv.showsHorizontalScrollIndicator = NO;
_sv.backgroundColor = [UIColor colorWithRed:220.0f/255 green:220.0f/255 blue:220.0f/255 alpha:1];
[self.navigationController.navigationBar addSubview:_sv];
//往导航条上面添加按钮
NSArray *array = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"汽车",@"搞笑",@"北京"];
for (int i=0; i<8; i++) {
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_button setTitle:array[i] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_button.titleLabel.font = [UIFont systemFontOfSize:14];
_button.tag = 1000+i;
_button.frame = CGRectMake(10+((_sv.frame.size.width)/array.count*2)*i, 0, _sv.frame.size.width/array.count, 44);
[_button addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
[_sv addSubview:_button];
}
UIButton *btn = (UIButton *)[_sv viewWithTag:1000+page];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
#pragma mark - tableView实例化
- (void)CreatTableView{
NSArray *array = @[@"news_toutiao",@"news_ent",@"news_sports",@"news_finance",@"news_tech",@"news_auto",@"news_funny",@"local_beijing"];
[MyConnection connectionWithUrl:[NSString stringWithFormat:@"http://api.sina.cn/sinago/list.json?channel=%@",array[page]] FinishBlock:^(NSData *data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dataDict = dict[@"data"];
NSArray *listArray = dataDict[@"list"];
for (NSDictionary *dic in listArray) {
if ([dic[@"category"] isEqualToString:@"hdpic"] && dic[@"is_focus"]){
Model1 *model = [[Model1 alloc] init];
model.title = dic[@"title"];
model.link = dic[@"link"];
model.kpic = dic[@"kpic"];
model.comment = dic[@"comments"];
model.totalComment = [dic[@"comment_count_info"] objectForKey:@"show"];
model.total = [dic[@"pics"] objectForKey:@"total"];
model.list = [dic[@"pics"] objectForKey:@"list"];//是数组
[_hdpicArray addObject:model];
}
else if (dic[@"is_focus"]){
Model1 *model = [[Model1 alloc] init];
model.title = dic[@"title"];
model.link = dic[@"link"];
model.kpic = dic[@"kpic"];
model.commentkey = dic[@"comments"];
model.totalComment = [NSString stringWithFormat:@"%@",[dic[@"comment_count_info"] objectForKey:@"show"]];
model.total = [dic[@"pics"] objectForKey:@"total"];
model.list = [dic[@"pics"] objectForKey:@"list"];//是数组
[_hdpicArray addObject:model];
}
if ([dic[@"category"] isEqualToString:@"cms"]) {
Model1 *model = [[Model1 alloc] init];
model.title = dic[@"title"];
model.kpic = dic[@"pic"];
model.intro = dic[@"intro"];
model.newsUrl = dic[@"link"];
model.commentkey = dic[@"comments"];
model.comment = [NSString stringWithFormat:@"%@评论",dic[@"comment"]];
[_cmsArray addObject:model];
}
}
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(page*winWidth, 0, winWidth, winHeight-113)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_bigSV];
[_bigSV addSubview:_tableView];
[_SVCon addObject:[NSString stringWithFormat:@"%g",_bigSV.contentOffset.x]];
} FailedBlock:^{
NSLog(@"请求失败");
}];
}
#pragma mark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
page = _bigSV.contentOffset.x/winWidth;
if (_scroView.contentOffset.x == _scroView.contentSize.width-320) {
_scroView.contentOffset = CGPointMake(0, 0);
}
num = _scroView.contentOffset.x/winWidth;
pc.currentPage = num;
for (int i=0; i<8; i++) {
UIButton *btn0 = (UIButton *)[_sv viewWithTag:1000+i];
[btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
UIButton *btn = (UIButton *)[_sv viewWithTag:1000+page];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_SVCon addObject:[NSString stringWithFormat:@"%g",_bigSV.contentOffset.x]];
//记录小sv的偏移量[[_SVCon lastObject] floatValue]
BOOL bl =[[NSString stringWithFormat:@"%g",_bigSV.contentOffset.x] isEqualToString:[NSString stringWithFormat:@"%g",[_SVCon[_SVCon.count-2] floatValue]]];
if (bl == NO) {
//页面改变了
if (scrollView.tag == 50) {
if (page<=4) {
_sv.contentOffset = CGPointMake((winWidth-50)/4*page, 0);
}else{
_sv.contentOffset = CGPointMake((winWidth-50)/4*4, 0);
}
[_cmsArray removeAllObjects];
[_hdpicArray removeAllObjects];
[self CreatTableView];
}
}
}
#pragma mark - tableView 事件回调
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Model1 *model = _cmsArray[indexPath.row-1];
ViewController *vc = [[ViewController alloc] init];
[_sv removeFromSuperview];
vc.stringUrl = model.newsUrl;
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (page>=0 && page<=5) {
return _cmsArray.count+1;
}
return _cmsArray.count+1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (page>=0 && page<=5) {
if (indexPath.row == 0) {
return 200;
}
return 70;
}
return 70;
}
//头条点击详情
- (void)tap{
HdpicDetailViewController *hdvc = [[HdpicDetailViewController alloc] init];
Model1 *model = _hdpicArray[num];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<model.list.count; i++) {
[arr addObject:[model.list[i] objectForKey:@"kpic"]];
[arr addObject:[model.list[i] objectForKey:@"alt"]];
}
hdvc.array = arr;
hdvc.totalComment = model.totalComment;
hdvc.commentsUrl = model.comment;
[_sv removeFromSuperview];//不移除进来先点它会有bug
[self presentViewController:hdvc animated:YES completion:nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (page>=0 && page<=5) {
if (indexPath.row == 0) {
AdCell *adcell = [tableView dequeueReusableCellWithIdentifier:@"qqq"];
if (!adcell) {
adcell = [[AdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qqq"];
}
if (_hdpicArray.count !=0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 200)];
[adcell addSubview:view];
_scroView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 200)];
[view addSubview:_scroView];
_scroView.pagingEnabled = YES;
_scroView.delegate = self;
_scroView.showsHorizontalScrollIndicator = NO;
_scroView.contentSize = CGSizeMake(winWidth*_hdpicArray.count+winWidth, 200);
for (int i=0; i<_hdpicArray.count; i++) {
Model1 *model = _hdpicArray[i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(winWidth*i, 0, winWidth, 200)];
[imageView setImageWithURL:[NSURL URLWithString:model.kpic]];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
[imageView addGestureRecognizer:tap];
[_scroView addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(winWidth*i, 180, winWidth, 21)];
label.alpha = 0.8;
label.font = [UIFont systemFontOfSize:13];
label.backgroundColor = [UIColor grayColor];
label.text = model.title;
[_scroView addSubview:label];
}
pc = [[UIPageControl alloc] initWithFrame:CGRectMake(winWidth-150, 170, 0, 0)];
pc.numberOfPages = _hdpicArray.count;
pc.userInteractionEnabled = NO;
pc.tag = 100;
pc.currentPageIndicatorTintColor = [UIColor redColor];
pc.pageIndicatorTintColor = [UIColor whiteColor];
[view addSubview:pc];
}
return adcell;
}
}
if (page>=0 && page<=5) {
NewsCell *cell= [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil] firstObject];
}
if (indexPath.row < _cmsArray.count+1) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(257, 40, 63, 29);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.row;
[cell addSubview:button];
Model1 *model = _cmsArray[indexPath.row-1];
cell.titleLabel.text = model.title;
cell.desLabel.text = model.intro;
[cell.commentButton setTitle:model.comment forState:UIControlStateNormal];
[cell.newiConImage setImageWithURL:[NSURL URLWithString:model.kpic]];
}
return cell;
}else{
NewsCell *cell= [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil] firstObject];
}
if (indexPath.row < _cmsArray.count) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(257, 40, 63, 29);
button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
Model1 *model = _cmsArray[indexPath.row];
cell.titleLabel.text = model.title;
cell.desLabel.text = model.intro;
[cell.commentButton setTitle:model.comment forState:UIControlStateNormal];
[cell.newiConImage setImageWithURL:[NSURL URLWithString:model.kpic]];
}
return cell;
}
}
//新闻页面评论的点击事件
- (void)buttonClick:(UIButton *)sender{
//[NSString stringWithFormat:@"http://api.sina.cn/sinago/comment.json?uid=9f689a839001ca66&hotcount=10&wm=b207&p=1&cmnt_id=%@&oldchwm=12030_0001&imei=862964020324830&action=list&from=6046095012&type=all&chwm=12030_0001",self.commentsUrl]
Model1 *model = _cmsArray[sender.tag];
[MyConnection connectionWithUrl:[NSString stringWithFormat:@"http://api.sina.cn/sinago/comment.json?uid=9f689a839001ca66&hotcount=10&wm=b207&p=1&cmnt_id=%@&oldchwm=12030_0001&imei=862964020324830&action=list&from=6046095012&type=all&chwm=12030_0001", model.commentkey]FinishBlock:^(NSData *data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dataDic = dic[@"data"];
NSArray *array = dataDic[@"cmntlist"];
Comment2ViewController *cvc = [[Comment2ViewController alloc] init];
cvc.dataArray = array;
[_sv removeFromSuperview];
cvc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:cvc animated:YES];
} FailedBlock:^{
NSLog(@"请求失败");
}];
}
- (void)viewWillAppear:(BOOL)animated{
// NSLog(@"88");
[self smallSV];
}
@end
Navigation
#import "AppDelegate.h"
#import "LimitViewController.h"
#import "SaleViewController.h"
#import "FreeViewController.h"
#import "SubjectViewController.h"
#import "HotViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//导航背景
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"顶部条背景.png"] forBarMetrics:UIBarMetricsDefault];
//导航字体属性
[[UINavigationBar appearance] setTitleTextAttributes:
@{
NSFontAttributeName:[UIFont systemFontOfSize:20.0],
NSForegroundColorAttributeName:[UIColor whiteColor]
}];
//限免
LimitViewController* limitVC = [[LimitViewController alloc] init];
//降价
SaleViewController* saleVC = [[SaleViewController alloc] init];
//免费
FreeViewController* freeVC = [[FreeViewController alloc] init];
//专题
SubjectViewController* subjectVC = [[SubjectViewController alloc] init];
//热榜
HotViewController* hotVC = [[HotViewController alloc] init];
NSMutableArray* array = [NSMutableArray arrayWithObjects:limitVC, saleVC, freeVC, subjectVC, hotVC, nil];
NSArray* titleArray = @[@"限免", @"降价", @"免费", @"专题", @"热点"];
for (int i = 0; i < array.count; i++) {
//遍历页面
UIViewController* vc = array[i];
//设置标题
vc.title = titleArray[i];
//添加导航
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:vc];
[array replaceObjectAtIndex:i withObject:nc];
//设置图标
vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_正常.png", titleArray[i]]];
//选中图标样式 修改渲染模式
vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_点击.png", titleArray[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
UITabBarController* tc = [[UITabBarController alloc] init];
//90 187 37
tc.tabBar.tintColor = [UIColor colorWithRed:90 / 255.0 green:187 / 255.0 blue:37 / 255.0 alpha:1];
tc.viewControllers = array;
self.window.rootViewController = tc;
[self.window makeKeyAndVisible];
return YES;
}
//得到剩余时间
- (NSString*)getExpireTime:(NSString*)expireTime{
//2015-03-25 13:05:31.0
//时间格式化
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//时间格式
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.S";
//转成日期类
NSDate* date = [formatter dateFromString:expireTime];
//剩余时间
int time = [date timeIntervalSinceNow];
//时
int h = time / 3600;
//分
int m = time % 3600 / 60;
//秒
int s = time % 60;
return [NSString stringWithFormat:@"剩余时间:%02d:%02d:%02d", h, m, s];
}
星星级别评价:
#import "StarView.h"
@interface StarView (){
UIView* _backView;
UIView* _foreView;
}
@end
@implementation StarView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self makeView];
}
return self;
}
- (void)awakeFromNib{
[self makeView];
}
- (void)makeView{
//15*14 75*14
_backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 14)];
[self addSubview:_backView];
_foreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 14)];
//裁剪子视图
_foreView.clipsToBounds = YES;
[self addSubview:_foreView];
for (int i = 0; i < 5; i++) {
//空心 1.主页_16.png
UIImageView* backStar = [[UIImageView alloc] initWithFrame:CGRectMake(i * 15, 0, 15, 14)];
backStar.image = [UIImage imageNamed:@"1.主页_16.png"];
[_backView addSubview:backStar];
//实心 1.主页_14.png
UIImageView* foreStar = [[UIImageView alloc] initWithFrame:CGRectMake(i * 15, 0, 15, 14)];
foreStar.image = [UIImage imageNamed:@"1.主页_14.png"];
[_foreView addSubview:foreStar];
}
}
- (void)setScore:(float)score{
_foreView.frame = CGRectMake(0, 0, 75 * (score / 5.0), 14);
}