2011-12-29 46 views
0

如何以編程方式選擇UIView的子視圖。以編程方式在iOS中選擇UIView的子視圖

Senario: 考慮一個UIView有10個UIImageView添加爲它的子視圖。 我如何通過手勢選擇其中的5個? 用戶在iPad上執行的手勢可能會選擇那5個UIImageViews? 如何以編程方式捕獲選擇?

請幫幫忙,

謝謝你, 的Suse。

回答

0

這裏有一個解決方案。你可以聲明一個名爲lastChosenViews的NSMutableArray。 (當然,你需要分配和初始化它的地方,也許在viewDidLoad或viewWillAppear方法)。

然後,您可以使用touchesEnded方法將觸摸的視圖添加到lastChosenViews數組中。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch=[touches anyObject]; 
    if ([lastChosenViews count]==5) 
     [lastChosenViews removeObjectAtIndex:0]; 
    [lastChosenViews addObject:touch.view]; 
} 

然後,你可以使用lastChosenViews數組,無論你想要的地方。

P.S.如果需要,也可以檢查touch.tapCount。對於特殊的手勢,使用UIGestureRecognizer的子類UIPinchGestureRecognizer,UIRotationGestureRecognizer,UISwipeGestureRecognizer,UIPanGestureRecognizer UILongPressGestureRecognizer

相關問題