2015-11-06 94 views
1

我正在使用手勢滾動視圖。我在Scroll View中添加了一個UIView,它的大小等於ScrollView的內容大小。我想在ScrollView的子視圖上應用捏手勢和旋轉手勢。我通過使用縮放屬性和ScrollView的委託來完成捏合手勢的工作,它給了我想要的相同效果。但旋轉手勢正在創造問題。當我在視圖上添加旋轉手勢時,滾動視圖的縮放也會受到干擾。UIScrollView上的旋轉手勢

那麼我怎樣才能應用捏的手勢和旋轉手勢滾動視圖的子視圖的大小必須等於最初的ScrollView的內容大小。

有人能給我這樣做的方式!

這是.m文件的代碼,當我們旋轉視圖它成爲無形

#import "ViewController.h" 

@interface ViewController() 
{ 
    UIView *backgroundView; 
    UIScrollView *scrollView; 
    CGFloat lastRotation; 
} 
@end 

@implementation ViewController 

-(void)loadView 
{ 
    [super loadView]; 

    //Scroll View 
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; 
    scrollView.contentSize = self.view.frame.size; 
    scrollView.delegate = self; 
    scrollView.backgroundColor = [UIColor grayColor]; 

    //Zooming factors of the Scroll View 
    scrollView.minimumZoomScale = 1.0; 
    scrollView.maximumZoomScale = 5.0f; 
    scrollView.zoomScale = 1.0; 

    [self.view addSubview:scrollView]; 

    //Scroll View's subview 
    backgroundView = [[UIView alloc] initWithFrame:scrollView.frame]; 
    [backgroundView setBackgroundColor:[UIColor orangeColor]]; 
    [scrollView addSubview:backgroundView]; 

    UIRotationGestureRecognizer *bgRotationGstr = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateBackgroundView:)]; 
    bgRotationGstr.delegate = self; 
    bgRotationGstr.cancelsTouchesInView = NO; 
    [backgroundView addGestureRecognizer:bgRotationGstr]; 

    //Child of background view 
    UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 100, 100)]; 
    childView.backgroundColor = [UIColor grayColor]; 
    [backgroundView addSubview:childView]; 

} 

//Rotation of the background view 
-(void)rotateBackgroundView:(UIRotationGestureRecognizer*)gesture 
{ 
    CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)gesture rotation]); 

    CGAffineTransform currentTransform = backgroundView.transform; 
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation); 

    [backgroundView setTransform:newTransform]; 

    lastRotation = [(UIRotationGestureRecognizer*)gesture rotation]; 

    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) 
    { 
     scrollView.scrollEnabled = NO; 
    } 
    else if (gesture.state == UIGestureRecognizerStateEnded) 
    { 
     lastRotation = 0.0; 
     scrollView.scrollEnabled = YES; 
     return; 
    } 
} 

#pragma mark<UIScrollViewDelegate> 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return backgroundView; 
} 

#pragma mark<UIGetsureRecognizer> 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    return YES; 
} 
@end 

回答

0

添加到您的.m

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
    return YES; 
} 

另外,還要確保你不要有獨佔觸摸啓用在任何手勢識別器上。

+0

已經做到了這一點,但旋轉沒有實施得當 – Saksha

+0

@約翰請張貼所有相關的代碼:) –

+0

請檢查代碼,並試圖幫助我:) – Saksha

0

嘗試在內容視圖的子視圖中放置可縮放/可旋轉的內容。 (也許你必須將你的內容視圖的「剪輯內容」屬性設置爲true - 不確定)

這種方式由於其內容視圖保持不變,所以不再擔心由轉換引起的滾動視圖。

如果您必須顯示剪輯內容(例如,如果您旋轉一個正方形,例如,角落會超出初始區域),請重新計算您的內容視圖並更新滾動視圖。

因爲你給的代碼,我建議嘗試:

//Scroll View's subview 
backgroundViewHolder = [[UIView alloc] initWithFrame:scrollView.frame]; 
[backgroundViewHolder setBackgroundColor:[UIColor orangeColor]]; 
[scrollView backgroundViewHolder]; 

//Holder View's subview 
backgroundView = [[UIView alloc] initWithFrame:backgroundViewHolder.bounds]; 
[backgroundViewHolder addSubview:backgroundView]; 

一切應保持不變。這只是一個想法......不知道這是正確的答案。

+0

不,我不申請剪裁,請檢查代碼並幫助 – Saksha

+0

我幫助了......「嘗試在內容視圖的子視圖中放置可縮放/旋轉的內容。」 - 你的代碼還沒有。 :) - 我編輯我的答案後果。 – Moose

+0

我試過這個,不工作:( – Saksha

-1
  1. 將此項添加到您的工具文件,將它作爲UIGestureRecognizerDelegate
 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
    return YES; 
} 
  • rotationGeustureRecognozier.delegate = self; //(器具文件)
  • 2

    後掙扎,我發現,以使用的最終溶液的多少天的UIScrollView集成縮放行爲與UIRotationGestureRecognizer一起工作就像一個魅力。您必須添加容器虛擬視圖作爲滾動視圖的子視圖,並將UIImageView作爲容器視圖的子視圖。之後,返回viewForZoomingInScrollView方法中的容器視圖,並將UIRotationGestureRecognizer添加到滾動視圖,將CGAffineTransformRotate應用於UIImageView。最後,在中同時返回true,同時使用KeepestureRecognizer方法。通過這種方式,scrollView將捕獲兩個手指旋轉手勢和捏到縮放手勢:縮放將應用於虛擬視圖並旋轉到uiimageview,而不會發生轉換之間的衝突。

    在代碼中:讓我們認爲有一個UIViewController呈現UIScrollView。我們希望使用scrollview的縮放行爲和UIImageView旋轉。

    1)包含UIScrollView的控制器(或任何其他對象)必須符合UIGestureRecognizerDelegate協議。

    在myViewController.h

    @interface myViewController : UIViewController < UIGestureRecognizerDelegate> { 
    } 
    

    2)創建一個UIScrollView,添加一個虛設視圖子視圖最後加一個UIImageView作爲僞視圖的子視圖。

    在myViewController.m

    //Scrollview 
    myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0, view.frame.size.width, view.frame.size.height)]; 
    myScrollView.delegate=self; 
    [view addSubview:myScrollView]; 
    
    //Dummy View 
    UIView *dummyView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, myScrollView.frame.size.width, myScrollView.frame.size.height)]; 
    [self addSubview:dummyView]; 
    
    //ImageView 
    imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, dummyView.frame.size.width, dummyView.frame.size.height)]; 
    imageView.contentMode=UIViewContentModeScaleAspectFit;   
    [dummyView addSubview:imageView]; 
    
    //Add rotation gesture to the scrollView 
    rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)]; 
    [myScrollView addGestureRecognizer:_rotationGestureRecognizer]; 
    
    //Set the controller as delegate of the recognizer 
    rotationGestureRecognizer.delegate=self; 
    
    
    [...] 
    
    
    #pragma UIScrollViewDelegate 
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    { 
        //Set the dummy view (imageview's superview) as view for zooming 
        return imageView.superview; 
    } 
    
    
    [...] 
    
    
    #pragma Mark - UIGestureRecognizerDelegate 
    
    - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    
        //Make it possibile to recognize simultaneously pinch and rotation gestures 
        return TRUE; 
    } 
    
    
    [...] 
    
    
    - (IBAction) handleRotate:(UIRotationGestureRecognizer*)recognizer { 
    
        //Apply the rotation to imageView 
        imageView.transform = CGAffineTransformRotate(imageView.transform, recognizer.rotation); 
        recognizer.rotation = 0;  
    } 
    

    對於simplyfing目的,我寫在同一個控制器的一切。你可以自由地繼承UIScrollView。請記住,技巧是:

    1)返回容器虛擬視圖爲viewForZoomingInScrollView所以縮放會影響容器視圖和旋轉會影響uiimageview。

    2)將包含scrollview的視圖控制器設置爲旋轉手勢識別器的代表,並返回TRUE爲shouldRecognizeSimultaneouslyWithGestureRecognizer

    +0

    你救了我的一天兄弟。在層次結構中添加'return imageView.superview;'和一個「虛擬視圖」是重點。 Tnx很多。 –