在UIScrollview和UIPageControl中展示的图片添加点击事件


在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];

    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值