轮播图点击 手势 代码

#import "HotNewsTableViewController.h"

#import "HotNewsTableViewCell.h"

#import "HotNews.h"

#import "JudegNetWorkType.h"

#import "HotNewsDetailViewController.h"

#import "MJRefresh.h"

#import "MBProgressHUD+Show.h"

//当前屏幕宽

#define kCurrWith [UIScreen mainScreen].bounds.size.width

@interface HotNewsTableViewController ()

{

BOOL flag;

NSInteger count;

}

@property (nonatomic, retain) NSMutableArray *allDataArray;

@property (nonatomic, retain) NSMutableArray *imgDataArray;

@property (nonatomic, retain) UILabel *label;

@end

@implementation HotNewsTableViewController

- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

self.title = @"今日热点";

//初始化数组

self.allDataArray = [NSMutableArray array];

//设置头View

self.tableView.tableHeaderView = [self setHeadView];

//设置尾View

self.tableView.tableFooterView = [self sendFootView];

[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(PageControlAction) userInfo:self repeats:YES];

//NSLog(@" kuan : %f",);

/**

*  集成刷新控件

*/

[self setupRefresh];

count = 20;

}

//触发滚动

- (void)PageControlAction

{

if (_allDataArray.count == 0) {

return;

}

//取模型

HotNews *hotNews = _allDataArray[_pageControl.currentPage + 1];

_label.text = hotNews.title;

//判断

if (_pageControl.currentPage != 3) {

//增加当前页

_pageControl.currentPage = _pageControl.currentPage + 1;

[_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * kCurrWith, 0) animated:YES];

return;

} else {

//为3时 -》 置0

_pageControl.currentPage = 0;

_scrollView.contentOffset = CGPointMake(0, 0);

return;

}

}

- (UIView *)setHeadView

{

UIView *allView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 170)] autorelease];

_scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 145)] autorelease];

//可滚动区域

_scrollView.contentSize = CGSizeMake(kCurrWith * 4, CGRectGetHeight(_scrollView.frame));

//添加手势

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];

[_scrollView addGestureRecognizer:tapGR];

_scrollView.pagingEnabled = YES;

_scrollView.delegate = self;

[allView addSubview:_scrollView];

// 标题

_label = [[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_scrollView.frame) + 5, kCurrWith, 20)] autorelease];

_label.font = [UIFont systemFontOfSize:13.0f];

[allView addSubview:_label];

UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title.jpg"]] autorelease];

imageView.frame = CGRectMake(10, 3, 15, 15);

[_label addSubview:imageView];

// 小圆点

_pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(200, 0, 150, 15)] autorelease];

_pageControl.numberOfPages = 4;

_pageControl.currentPageIndicatorTintColor = [UIColor redColor];

_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.219 green:0.185 blue:1.000 alpha:1.000];

[_pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];

[_label addSubview:_pageControl];

return allView;

}

- (UIView *)sendFootView

{

UIView *allView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, kCurrWith, 30)] autorelease];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(0, 0, kCurrWith, 30);

[button setTitle:@"点击加载更多" forState:UIControlStateNormal];

button.alpha = 0.7f;

[button addTarget:self action:@selector(jiazaiButtonAction:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColor blueColor];

[allView addSubview:button];

return allView;

}

- (void)jiazaiButtonAction:(UIButton *)sender

{

if ([JudegNetWorkType getNetWorkType] == BadNetWorkLink) {

return;

}

NSString *str = [listUrlOne stringByAppendingString:[NSString stringWithFormat:@"%ld-20.html", (long)count]];

NSURL *url = [NSURL URLWithString:str];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

__block HotNewsTableViewController *hotNewsTVC = self;

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

if (dict.count != 0) {

for (NSDictionary *item in dict[@"T1348647909107"]) {

HotNews *hotNews = [HotNews new];

if (![item[@"digest"] isEqualToString:@""]) {

[hotNews setValuesForKeysWithDictionary:item];

[_allDataArray addObject:hotNews];

[hotNews release];

}

}

}

//跟新页面

dispatch_async(dispatch_get_main_queue(), ^{

[self.tableView reloadData];

});

}];

count += 20;

}

//处理轮播图 点击 手势

- (void)tapGRAction:(UITapGestureRecognizer *)sender

{

for (int i = 0; i < 4; i ++) {//i 代表第几个模型

//contentOffset  是scrollview当前显示区域顶点相对于frame顶点的偏移量

if (_scrollView.contentOffset.x == i * kCurrWith) {

HotNewsDetailViewController *hotNewsDetailVC = [[[HotNewsDetailViewController alloc] init] autorelease];

HotNews *test = _allDataArray[i];

hotNewsDetailVC.hotNews = test;

hotNewsDetailVC.docid = test.docid;

[self.navigationController pushViewController:hotNewsDetailVC animated:YES];

}

}

}

////

//- (void)pageControlAction:(UIPageControl *)sender

//{

//    [_scrollView setContentOffset:CGPointMake(sender.currentPage * kCurrWith, 0) animated:YES];

//     NSLog(@"-------------------");

//}

/**

*  集成刷新控件

*/

- (void)setupRefresh

{

// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)

[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];

[self.tableView headerBeginRefreshing];

//    if (_allDataArray.count == 0) {

//        [self.tableView headerBeginRefreshing];

//    }

// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)

// 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)

self.tableView.headerPullToRefreshText = @"下拉可以刷新了";

self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";

self.tableView.headerRefreshingText = @"正在帮你刷新中...";

self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";

self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";

self.tableView.footerRefreshingText = @"正在帮你加载中...";

}

#pragma mark - 发送网络请求,显示数据

- (void)headerRereshing

{

if ([JudegNetWorkType getNetWorkType] == BadNetWorkLink) {

NSLog(@"网络异常,头条数据显示不出来");

} else{

if (_allDataArray.count == 0) {

NSURL *url = [NSURL URLWithString:HotNews_List_URL];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

__block HotNewsTableViewController *hotTVC = self;

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

if (dict.count != 0) {

for (NSDictionary *item in dict[@"T1348647909107"]) {

HotNews *hotNews = [HotNews new];

if (![item[@"digest"] isEqualToString:@""]) {

[hotNews setValuesForKeysWithDictionary:item];

[_allDataArray addObject:hotNews];

[hotNews release];

}

}

}

//跟新页面

dispatch_async(dispatch_get_main_queue(), ^{

[hotTVC updateDataForUI];

});

}];

} else {

NSURL *url = [NSURL URLWithString:HotNews_List_URL];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

__block HotNewsTableViewController *hotTVC = self;

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

NSMutableArray *newHead = [NSMutableArray array];

if (dict.count != 0) {

for (NSDictionary *item in dict[@"T1348647909107"]) {

HotNews *hotNews = [HotNews new];

if (![item[@"digest"] isEqualToString:@""]) {

[hotNews setValuesForKeysWithDictionary:item];

}

for (int i = 0; i<_allDataArray.count; i++) {

if ([((HotNews *)_allDataArray[i]).title isEqualToString:hotNews.title]) {

flag = YES;

}

}

if (!flag) {

[newHead addObject:hotTVC];

flag = NO;

}

[hotNews release];

}

}

for (HotNews *hotNews in newHead) {

[_allDataArray insertObject:hotNews atIndex:0];

}

//跟新页面

dispatch_async(dispatch_get_main_queue(), ^{

[hotTVC updateDataForUI];

});

}];

}

}

}

#pragma mark 更新页面

- (void)updateDataForUI

{

[self.tableView reloadData];

[self.tableView headerEndRefreshing];

[MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];

[MBProgressHUD showSuccessWithText:@"请求成功"];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return _allDataArray.count - 4;

}

// 显示内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *cellIdentifier = @"cellIdentifier";

HotNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {

cell = [[[HotNewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

}

if (_allDataArray.count) {

HotNews *hotNews = _allDataArray[indexPath.row + 4];

NSURL *imgUrl = [NSURL URLWithString:hotNews.imgsrc];

[cell.imgsrcView sd_setImageWithURL:imgUrl];

cell.hotNews = hotNews;

}

// 滚动图片新闻

for (int i = 0; i < 4; i++) {

HotNews *hotNews = _allDataArray[i+4];

NSURL *imgUrl = [NSURL URLWithString:hotNews.imgsrc];

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

[imageView sd_setImageWithURL:imgUrl];

imageView.frame = CGRectMake(CGRectGetWidth(_scrollView.frame) * i, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame));

[_scrollView addSubview:imageView];

}

HotNews *hotNews = _allDataArray[0];

_label.text = [NSString stringWithFormat:@"      %@", hotNews.title];

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

HotNewsDetailViewController *hotNewsDetailVC = [[[HotNewsDetailViewController alloc] init] autorelease];

HotNews *test = _allDataArray[indexPath.row + 4];

hotNewsDetailVC.hotNews = test;

hotNewsDetailVC.docid = test.docid;

[self.navigationController pushViewController:hotNewsDetailVC animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 85;

}

@end

时间: 2024-08-10 10:35:00

轮播图点击 手势 代码的相关文章

JS 轮播图(无缝连接的轮播图实现,含代码供参考)

需求:实现轮播图,图片无缝切换,自动播放. 实现效果: 思考一下:在图片列表后面多加一张图片,这张图片是第一张图片(为了确保无缝衔接).图片就是平铺放在一个pic里面的,每次移动就是改变的pic的left值. 来撸代码~~.所有的代码放在最后面,这里只讲一些重要的方法: 为防止懵逼:先贴出封装函数move()的代码供参考 function move(ele, attr, speed, target, callback){ //获取当前的位置 //从左往右进行移动 --- current<targ

类似轮播图的简单jQuery代码

代码和个人解析: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible&

自定义View实现广告位轮播图barner组件

闲谈 最近公司事情不算太多,闲来无事,看到项目中用到的广告轮播图,之前都是使用第三方的,趁事情不算多,所以自己实现一个广告位轮播图barner组件,这样的话,在以后的开发中就可以使用自己的了. 知识点 好了,切入正题!我们要想实现barner组件,首先要求我们需要哪些知识点呢? 1.自定义View的流程(测量.布局.绘制) 2.广告位轮播图滑动的时候,我们需要弹性滑动Scroller 3.自定义View的事件传递机制 4.在我们自定义View事件传递给我们自定义的View的时候,我们在OnTou

js学习总结----轮播图之渐隐渐现版

具体代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0; padding:0; font-size:14px; -webkit-user-select:none; } ul,li{ list-style } im

从零开始,搭建博客系统MVC5+EF6搭建框架(5),博客详情页、留言、轮播图管理、右侧统计博文

一.博客系统进度回顾 上一遍博客介绍到,系统已经实现到了发布以及前台布局展示,接下来就是实现一些,详情页,留言.轮播图管理.右侧博文统计信息实现. 二.博客系统详情页实现 2.1先来看看详情页展示的效果 2.2实现控制器在前台控制器中创建一个Blog的控制器,主要是展示博客分类以及详情页 Action详情页实现: 1 /// <summary> 2 /// 详情页 3 /// </summary> 4 /// <param name="id"><

js实现图片轮播图(一)

一.实现原理 (1)将所有图片放在一个父容器div里面,通过display属性来设置图片的出现与隐藏: (2)轮播图分为手动轮播和自动轮播: 手动轮播的重点是每次点击图片下方的小圆圈,获得它的索引号,并让与之对应索引号的图片显示,并且此时的小圆圈为高亮显示: 自动轮播:利用定时器setInterval(),来每隔一定的时间来播放一次图片. (3)所有的基础知识:dom操作,定时器,事件运用. 二.轮播图页面布局: <div id="content">   <!-- 总

告别组件之教你使用原生js和css写移动端轮播图

在工作中由于项目需要要写一个轮播图,本想使用组件直接调用实现快速开发,但是一想到自己经常使用组件但是让自己手写的话确实一点都不会. 一个不会手写组件的前端程序员不是一个好程序员!于是打算自己手写一个. 老规矩,首先看一下最终效果,这个最终可以实现定时自动播放,触摸滑动,手动修改下面横条效果等功能. 项目中使用到的HTML代码如下 <div class="banner"> <ul class="clearfix"> <li><

妈蛋:kinMaxShow轮播图异常,WebUploader图片上传坑爹,图片被压缩了

今天晚上在改造轮播图. 原来的代码是这样的: <div> <img src="${static}/image/index/banner/`.jpg" /> </div> <div> <img src="${static}/image/index/banner/2.jpg" /> </div> <div> <img src="${static}/image/index/

jquery特效(3)—轮播图①(手动点击轮播)

今天上班做设计做的头疼,就写了一个轮播图练练手,先写了一个手动点击轮播的轮播图,随后我会慢慢接着深入写自动轮播图和鼠标悬浮图片停止移动轮播图等~~~~~~虽然今天我生日,但是代码还是得写的,不能找借口放松自己,原地踏步也算后退~~~~ 下面来看看最终做的手动点击轮播效果: 哈哈哈,有没有看出来我的四张图片都是美男子哦~~~~找个赏心悦目的图片真不容易,这样才能激起我写轮播图的代码欲望,写出来就可以点击按钮翻他们牌子了~~~~ 一.原理说明 (1)首先是轮播图的架构,我采用了一个最外边的大div包