How to zoom a UIScrollView inside of a UICollectionViewCell?

后端 未结 7 977
天涯浪人
天涯浪人 2020-12-04 20:10

I\'m trying to add a UIScrollView inside of a UICollectionViewCell. The idea is that you can use pinch to zoom the UIScrollView (and w

相关标签:
7条回答
  • 2020-12-04 20:19

    The delegates are not getting called because you added it inside the UICollectionview.The touch events are availabel for the collection view which is the superview and hence not obtainable by the view inside.You may have to think of some other way to achieve this model.

    UICollectionView is having the same pattern as UITableView,The problem occours in tableview inside scrollview ,that the touch event is accepted by scrollview[which is the superview] and to make the tableview scrollable in that case ,The scroll of scrollview must be disabled.This is another version of that problem

    The apple docs says that the above said case make unpredictable results.So This may be same for your problem.

    In my opinion you must go for better design which can achieve what you look for

    0 讨论(0)
  • 2020-12-04 20:25

    Please add scrollview in your cell and your current cell image view add in scrollview. then use below code:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        ImageContext *context = [self.images objectAtIndex:indexPath.row];
        ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
        cell.cellScrollView.autoresizesSubviews = YES;
        cell.cellScrollView.multipleTouchEnabled =YES;
        cell.cellScrollView.maximumZoomScale = 4.0;
        cell.cellScrollView.minimumZoomScale = 1.0;
        cell.cellScrollView.clipsToBounds = YES;
        cell.cellScrollView.delegate = self;
        cell.cellScrollView.zoomScale = 1.0;
    
        [cell.imageView setImage:[UIImage imageNamed:context.thumbImageUrl]];
    
    return cell;
    }
     -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
     {   NSLog(@"%i",scrollView.subviews.count);
      for (UIView *v in scrollView.subviews) {
        if ([v isKindOfClass:[UIImageView class]]) {
            return v;
          }
      }
    

    }

    0 讨论(0)
  • 2020-12-04 20:29
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {            
      if (touch.view == [self myScrollView]) //<- whatever your scrollView is called 
        {
          return YES;
        }
      return NO;
    }
    

    Don't know your code but try playing around with the above code to see if you can filter your touches to the objects you want. The above code is from the UIGestureRecognizerDelegate Protocol Reference.

    0 讨论(0)
  • 2020-12-04 20:37

    I just made an implementation for SWIFT 3 on iOS 9.3+ and all i done was:

    1. Place the image view inside a scrollview

    2. Connect the scrollview delegate to collectionview cell class

    3. Implement the code below on the collectionview subclass

    class FullScreenImageTextDetailCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {
    
         @IBOutlet var scrollview: UIScrollView!
         @IBOutlet weak var backgroundImageView: UIImageView!
    
         override func awakeFromNib() {
             self.scrollview.minimumZoomScale = 0.5
             self.scrollview.maximumZoomScale = 3.5
             self.scrollview.delegate = self
         }
    
         func viewForZooming(in scrollView: UIScrollView) -> UIView? {
             return self.backgroundImageView
         }
    }
    

    No gesture recognizer adding or removing on the parent collectionview controller was necessary, worked like a charm!

    Thanks for previous examples for reaching this!

    0 讨论(0)
  • 2020-12-04 20:37

    Check if all related view's multitouch is on. iOS disabled multitouch by default on most views to save energy.

    0 讨论(0)
  • I've set the delegate of the UIScrollView to be the UICollectionViewCell, but none of the delegate methods are being called.
    

    For that just define one function in collectionViewCell

      @IBOutlet weak var mainScrollView:UIScrollView!
      func setup(){
            mainScrollView.minimumZoomScale=1
            mainScrollView.maximumZoomScale=10.0
    
            mainScrollView.delegate=self
    
        }
    
       func viewForZooming(in scrollView: UIScrollView) -> UIView? {
            return yourimageviewinside of scrollview
        }
    

    call this fuction in collectionview(_,cellForItem ) method of collectionview

    0 讨论(0)
提交回复
热议问题