簡體   English   中英

如何在iOS的imageview中添加手勢識別器?

[英]How to add a Gesture Recogniser in imageview in iOS?

我將UIPanGestureRecognizer添加到我的imageview中,然后添加了Gesture Recognizer,但我的imageview圖像平移到了我​​的視圖中,我只希望在該視圖中的imageview內平移圖像,這是怎么可能的?

我在viewdidload上為此寫了一個代碼

 UIPanGestureRecognizer *pangesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureDetected:)];
[pangesture setDelegate:self];
[self.zoomImage addGestureRecognizer:pangesture];

方法是

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
    CGPoint translation = [recognizer translationInView:recognizer.view];
    [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}
}

然后在我的視圖中平移imageView圖像,但我希望僅在imageview內部平移圖像,如果可能的話,請給我解決方案。 這里我原來的imageview大小就像 在此處輸入圖片說明

當我添加UIPanGEstureRecognizer時,它看起來像
在此處輸入圖片說明 圖片在“視圖”中平移,但我想在imageview尺寸內進行縮放,請為此提供解決方案。

采取UIScrollView並在scrollview內添加UIImageView並在scrollview上進行手勢。

為滾動視圖設置委托並執行代碼

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that we want to zoom
    return self.zoomImage;
}

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

- (void)centerScrollViewContents {
    CGSize boundsSize = scrollView.bounds.size;
    CGRect contentsFrame = self.zoomImage.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.zoomImage.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // Get the location within the image view where we tapped
    CGPoint pointInView = [recognizer locationInView:imageView];

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

    // Figure out the rect we want to zoom to, then zoom to it
    CGSize scrollViewSize = scrollView.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);

    [scrollView zoomToRect:rectToZoomTo animated:YES];
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, scrollView.minimumZoomScale);
    [scrollView setZoomScale:newZoomScale animated:YES];
}

UIImage對象的userInteractEnable默認情況下為NO,因此您無法與其交互。 嘗試self.zoomImage.userInteractEnable = YES

嘗試這個:
添加另一個UIViewimageView ,使其大小完全一樣的imageView 然后設置您的imageView ,因為這UIView的子視圖通過拖放imageView這個頂部UIView (假設你使用的是StoryBoardxib )。
之后,如果您使用的是StoryBoardxib ,則轉到Attributes Inspector,選擇此UIView ,然后Click Subviews 如果不是,則設置view.clipsToBounds = YES;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM