在UIScrollview和UIPageControl中展示的图片添加点击事件,手势要放在循环体中
-(void)topImage{
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
NSString *loginKey=[userDefaults stringForKey:@"loginKey"];
NSString *string = [NSString stringWithFormat:@"%@xwdz/api/topline?access_token=%@&category_id=%ld", BaseURLString,loginKey,(long)classIdString];
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [request setTimeoutInterval:5];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.photodocs=(NSMutableDictionary *)responseObject;
self.photoinfos=[self.photodocs info];
//添加推荐图片展示
CGRect r = [[UIScreen mainScreen] applicationFrame];
//推荐页的图片展示设置
_bannerScroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, r.size.width, r.size.width*479/959)];
_bannerScroller.scrollEnabled=YES;
_bannerScroller.delegate=self;
_bannerScroller.bounces=NO;
_bannerScroller.directionalLockEnabled=YES;
_bannerScroller.scrollsToTop=NO;
//分页设置
_bannerScroller.pagingEnabled=YES;
//滚动条显示设置
_bannerScroller.showsHorizontalScrollIndicator=NO;
_bannerScroller.showsVerticalScrollIndicator=NO;
//试图内容尺寸
_bannerScroller.contentSize=CGSizeMake(r.size.width*[self.photoinfos count], r.size.width*479/959);
_bannerScroller.delegate=self;
self.tableView.tableHeaderView =_bannerScroller;
//添加内容
float x=0;
for (int i=0; i<[self.photoinfos count]; i++) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, 0, r.size.width, r.size.width*479/959)];
NSDictionary *doc = self.photoinfos[i];
imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:doc.pic]]];
[imageView setTag:[doc.uid intValue]];
imageView.userInteractionEnabled=YES;
//添加图片点击手势
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(todetail:)];
[imageView addGestureRecognizer:tapGesture];
[_bannerScroller addSubview:imageView];
UILabel *titleLabelscro=[[UILabel alloc]initWithFrame:CGRectMake(x, r.size.width*479/959-20, r.size.width, 20)];
titleLabelscro.text=doc.title;
titleLabelscro.numberOfLines=1;
titleLabelscro.textColor=[UIColor whiteColor];
titleLabelscro.font=[UIFont boldSystemFontOfSize:16];
titleLabelscro.backgroundColor=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.9];
[_bannerScroller addSubview:titleLabelscro];
x+=r.size.width;
}
self.tableView.tableHeaderView =_bannerScroller;
//创建分页控制器,添加到tableview
_pageScroller=[[UIPageControl alloc]initWithFrame:CGRectMake(0, r.size.width*479/959-20, r.size.width, 20)];
_pageScroller.numberOfPages=[self.photoinfos count];
_pageScroller.backgroundColor=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0];
[self.tableView addSubview:_pageScroller];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation start];
}