2013-05-17 132 views
1

有幾個帖子與此類似,但我無法將其他響應修改爲我的代碼。我在使用我的scrollview中的方法touchesBegantouchesMovedtouchesEnded時遇到問題。在UIScrollView中觸摸交互

我目前有一個大的scrollview(3072 x 2304)。我有一個UIImageView作爲subview這一點,並希望檢測任何一個觸摸(無關緊要,因爲它會爲我產生相同的結果)。

這是我目前的做法是:

//Set scrollview to size of image 
self.myScrollView.contentSize = CGSizeMake(3072, 2304); 

//Load image in big rect 
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 3072, 2304)]; 

//set image to pic in bundle 
myImageView.image = [UIImage imageNamed:@"dot.jpg"]; 

//make imageview subview of scrollview 
[self.myScrollView addSubview: myImageView]; 

//Making subview for touches 
CGRect frame = CGRectMake(0, 0, 3072, 2304); 
touchView = [[UIScrollView alloc] initWithFrame:frame]; 
[self.myScrollView addSubview: touchView]; 

//Allow for 2 touches to move screen 
for (UIGestureRecognizer *gestureRecognizer in myScrollView.gestureRecognizers) 
{ 
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) 
    { 
     UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer; 
     panGR.minimumNumberOfTouches = 2;    
    } 
} 

注意:建議使用手勢識別器是不理想的

方法。 我知道,除touchesMoved之外別無他法;實現這個唯一可行的方法是UIScrollView的子類,但是這沒有帶給我喜悅。

回答

0

嘗試使用這一個。你知道imageview的用戶交互是默認的false所以你需要設置它true

myImageView.userInteractionEnabled = YES; 

而你需要手勢viewDidLoad中

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    gestureRecognizer.delegate = self; 
    [scrollView addGestureRecognizer:gestureRecognizer]; 
} 

-(void) handleGesture:(UIGestureRecognizer *) sender 
{ 
//------- do here 
} 
+0

我真的不理解這個添加到您的滾動視圖。當然,UITapGestureRecognizer只能檢測單點觸摸事件?我需要能夠跟蹤觸摸動作。 –

+0

在這種情況下,您需要將該手勢也添加到您的滾動視圖 –

+0

我已經嘗試使用此方法。問題在於UITapGestureRecognizer在觸地事件後無法檢測到移動 - 它的行爲與touchesBegan相似。我需要像touchesMoved那樣的東西。 –