[英]Adding sliding UIImageView on customcell
當我翻轉UIImageview時,我想在UIImageview中更改圖像,我在customcell上添加UIScroll視圖,在滾動視圖上我添加了UIImageview我還想添加UIPagecontrol,當我在imageview圖像上滑動時正在改變但是在頁面中點控制器沒有改變,請任何人幫助我是ios的新手,如何在customcell上添加分頁滑動imageview? 以及如何使分頁點工作,這是我的代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
customCellForText *cell = [tableObj deq ueueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"customCellForText" owner:self options:nil];
cell = [nib objectAtIndex:0];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(130, 80, 150, 100)];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
[cell.contentView addSubview:scrollView];
for (int i = 0; i < imageArray.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [imageArray objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(210, 180, 30, 20)];
pageControl.backgroundColor = [UIColor redColor];
pageControl.numberOfPages = imageArray.count;
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:pageControl];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap)];
tapGesture.numberOfTapsRequired = 2;
[subview setUserInteractionEnabled:YES];
[subview addGestureRecognizer:tapGesture];
}
[cell.commentBtn addTarget:self action:@selector(commentBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.commentBtn.tag = indexPath.row;
cell.nameLabel.text=@"amol";
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
您可能需要在滑動時設置pageControl的currentPage屬性。
您也可以使用此公式計算新頁面,如下所示
NSInteger newPage = (NSUInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.