2017-02-03 48 views
0

我有10個圖像,我必須在滑動或滾動上進行更改,並使用雙擊放大特定位置,平移將在圖像放大時起作用,否則不會並排並排捏手勢應該保持。圖像陣列中的所有手勢

爲此,我應用了邏輯,但在滑動和平移手勢之間存在衝突,因此我放棄了滑動的概念,我使用scrollview和imageview放置在裏面,但是當一個圖像放大並且水平滾動時,下一個圖像也會放大。

雖然我把條件放在滾動視圖上的相同大小的圖像滾動但不會得到工作。

我用collectionview和內部單元格我放置scrollview然後圖像和應用所有的手勢,但不會工作。

+0

暗示:設置代表與和實施'UIGestureRecognizer'委託方法。 –

+0

嘗試CollectionView。這裏是一個示例: http://stackoverflow.com/questions/33546395/is-possible-to-zoom-in-out-a-uiimageview-in-custom-cell-of-a-uicollectionview-on – krishnanunni

+0

我試過該代碼,但它的不工作。提及的問題也.. .. @ krishnanunni –

回答

0

根據krishnanunni的回答鏈路:「is possible to zoom in/out a UIImageView in custom cell of a UICollectionView only when pinch the cell's imageView?」。我修改一下,效果很好Arrangement Same as from Sample

in gallerycell.m 

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview; 
@property (weak, nonatomic) IBOutlet UIImageView *imageview; 

- (void)setup; 

@end 


//cell.m 

#import "Cell.h" 
#define MAXIMUM_SCALE 3.0 
#define MINIMUM_SCALE 1.0 

@interface Cell()<UIScrollViewDelegate> 

@end 

@implementation Cell 


- (void)setup { 

    CGFloat newScale = MINIMUM_SCALE; 
    CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale); 
    self.imgView.transform = transform; 
    self.scrView.contentSize = self.imgView.frame.size; 

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
    doubleTap.delegate = self; 
    [self.scrView addGestureRecognizer:doubleTap]; 
    [doubleTap setNumberOfTapsRequired:2]; 
    ZoomCheck = YES; 


    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomImage:)]; 
    pinch.delegate = self; 
    self.imgView.gestureRecognizers = @[pinch]; 
    self.imgView.userInteractionEnabled = YES; 
    self.scrView.delegate = self; 

} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return self.imgView; 
} 
- (void)scrollViewDidZoom:(UIScrollView *)scrollView { 
    // The scroll view has zoomed, so we need to re-center the contents 
    [self centerScrollViewContents]; 
} 

- (void)centerScrollViewContents { 
    // This method centers the scroll view contents also used on did zoom 
    CGSize boundsSize = self.scrView.bounds.size; 
    CGRect contentsFrame = self.imgView.frame; 

    if (contentsFrame.size.width < boundsSize.width) { 
     contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
    } else { 
     contentsFrame.origin.x = 0.0f; 
    } 

    if (contentsFrame.size.height < boundsSize.height) { 
     contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
    } else { 
     contentsFrame.origin.y = 0.0f; 
    } 

    self.imgView.frame = contentsFrame; 
} 


- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture { 
    NSLog(@"TAPCALLED"); 
    CGPoint location = [gesture locationInView:self.imgView]; 
    NSLog(@"x %f y %f",location.x, location.y); 
    CGFloat currentScale = self.frame.size.width/self.bounds.size.width; 

    NSLog(@"%.f",currentScale); 
    CGPoint pointInView = [gesture locationInView:self.imgView]; 
    if(ZoomCheck){ 

     // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view 
     CGFloat newZoomScale = self.scrView.zoomScale * 3.0f; 
     newZoomScale = MIN(newZoomScale, self.scrView.maximumZoomScale); 

     // Figure out the rect we want to zoom to, then zoom to it 
     CGSize scrollViewSize = self.scrView.bounds.size; 

     CGFloat w = scrollViewSize.width/newZoomScale; 
     CGFloat h = scrollViewSize.height/newZoomScale; 
     CGFloat x = pointInView.x - (w/2.0f); 
     CGFloat y = pointInView.y - (h/2.0f); 

     CGRect rectToZoomTo = CGRectMake(x, y, w, h); 

     [self.scrView zoomToRect:rectToZoomTo animated:YES]; 


     ZoomCheck=NO; 
    } 
    else{ 

     CGFloat newZoomScale = self.scrView.zoomScale/3.0f; 
     newZoomScale = MIN(newZoomScale, self.scrView.minimumZoomScale); 

     // Figure out the rect we want to zoom to, then zoom to it 
     CGSize scrollViewSize = self.scrView.bounds.size; 

     CGFloat w = scrollViewSize.width * newZoomScale; 
     CGFloat h = scrollViewSize.height * newZoomScale; 
     CGFloat x = pointInView.x - (w * 2.0f); 
     CGFloat y = pointInView.y - (h * 2.0f); 

     CGRect rectToZoomTo = CGRectMake(x, y, w, h); 

     [self.scrView zoomToRect:rectToZoomTo animated:YES]; 


     ZoomCheck=YES; 
    } 


} 

//----------------------------------------------------------------------- 

#pragma mark - Custom Methods 

- (void)zoomImage:(UIPinchGestureRecognizer *)gesture { 

    if (UIGestureRecognizerStateBegan == gesture.state || 
     UIGestureRecognizerStateChanged == gesture.state) { 

     // Use the x or y scale, they should be the same for typical zooming (non-skewing) 
     float currentScale = [[gesture.view.layer valueForKeyPath:@"transform.scale.x"] floatValue]; 

     // Variables to adjust the max/min values of zoom 
     float minScale = 1.0; 
     float maxScale = 3.0; 
     float zoomSpeed = .5; 

     float deltaScale = gesture.scale; 

     // You need to translate the zoom to 0 (origin) so that you 
     // can multiply a speed factor and then translate back to "zoomSpace" around 1 
     deltaScale = ((deltaScale - 1) * zoomSpeed) + 1; 

     // Limit to min/max size (i.e maxScale = 2, current scale = 2, 2/2 = 1.0) 
     // A deltaScale is ~0.99 for decreasing or ~1.01 for increasing 
     // A deltaScale of 1.0 will maintain the zoom size 
     deltaScale = MIN(deltaScale, maxScale/currentScale); 
     deltaScale = MAX(deltaScale, minScale/currentScale); 

     CGAffineTransform zoomTransform = CGAffineTransformScale(gesture.view.transform, deltaScale, deltaScale); 
     gesture.view.transform = zoomTransform; 

     // Reset to 1 for scale delta's 
     // Note: not 0, or we won't see a size: 0 * width = 0 
     gesture.scale = 1; 
    } 

    } 


@end 

注:Zoomcheck是一個布爾值,以檢查是否是doubletapped和以前放大。

添加UICollectionView並在細胞內增加滾動視圖然後imageview.the安排示於以上鍊接

0

嘗試使用滾動視圖代表。不要只使用雙擊縮放和輕掃手勢來使用平移手勢。它工作正常。

+0

,我知道,但我也不會潘也。幫助我與該 –

+0

滾動查看代表可以幫助你平移。使用 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return yourImageView; } –