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

    
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章