2012-07-27 85 views
1

我有一個UIScrollView內的圖像,我有代碼,當用戶拖動他們的手指時繪製文本。我希望能控制,當用戶觸摸屏幕時,如果UIScrollView觸摸和傾聽事件

一)用戶應該使用他們的手指畫或

b)使用者應圍繞移動滾動視圖用他們的手指

我有一個布爾值來跟蹤用戶正在做什麼。我碰觸過的方法有:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    if(draw == false) { 
     printf("calling super"); 
     [scrollView touchesBegan:touches withEvent:event]; 
    } 
    else 
     [myPath moveToPoint:[mytouch locationInView:self]]; 

} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    if(draw == false) 
     [scrollView touchesMoved:touches withEvent:event]; 
    else { 
     [myPath addLineToPoint:[mytouch locationInView:self]]; 
     [self setNeedsDisplay]; 
    } 

爲什麼不能正常工作?基本上,如果我不繪製,我會調用touchesBegan和touchesMoved滾動視圖。如果我正在繪圖,我使用myPath繪圖。

但是,當draw爲false時,滾動視圖不會移動或放大等等,因爲它應該是這樣。

回答

1

我以前遇到過這個問題。我解決這樣的問題:

你應該做一個mainView。它有2個屬性。一個是yourScrollView,另一個是yourDrawImageView。 [yourScrollView addSubviews:yourDrawImageView];[mainView addSubviews:yourScrollView];

,然後寫你的觸摸方法在這樣的mainView.m(ignor了滾動statment)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
    { 

     [myPath moveToPoint:[mytouch locationInView:self]]; 
    } 

} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
    { 

     [myPath addLineToPoint:[mytouch locationInView:self]]; 
     [self setNeedsDisplay]; 
    } 


} 

的最後一步,是什麼s you ignor is to code scrollView touches event , it S IN yourSceollView.m寫,就像這樣:

#import "yourScrollView.h" 


@implementation yourScrollView 


- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesBegan:touches withEvent:event]; 
    if(!self.dragging) 
     [[self nextResponder] touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesMoved:touches withEvent:event]; 
    if(!self.dragging) 
     [[self nextResponder] touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesEnded:touches withEvent:event]; 
    if(!self.dragging) 
     [[self nextResponder] touchesEnded:touches withEvent:event]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

我想如果我集中滾動啓用虛假它可以幫助你:-)

0

UIScrollView使用UIGestureRecognizer s,由系統分別從touchesBegan:withEvent:和朋友中調用。一個更簡單的方法就是當你不想讓它對觸摸做出反應時,簡單地禁用滾動視圖上的滾動。

+0

,它似乎仍然可以滾動使用兩個手指來完成。 – CodeGuy 2012-07-27 19:36:27

+0

這讓我很驚訝。您是否也需要禁用縮放功能(通過設置maximumZoomScale = minimumZoomScale?也可以直接禁用其「panGestureRecognizer」)。 – 2012-07-27 19:46:13