2014-02-10 62 views
0

所有子視圖,的TouchEvent在UIScrollView中

我有一個問題,我已經看過了所有可能的答案在計算器上(和其它互聯網),但我嘗試所有的解決方案,沒有任何工程....

我有一個UIImageView作爲子視圖來創建一個板的UIScrollView。我可以放大和縮小,工作正常。也滾動工作正常!

現在,我想拖拽&將其他UIImageViews(圖塊)拖放到ScrollView中,同時也將ScrollView拖出。但是當我將UIImageView拖放到ScrollView中時,我無法拖放子視圖。

我已經在viewDidLoad中設置了setCanCancelContentTouches:UIScrollView的NO。
我已經創建了一個子類TileImageView,並在init中將exclusiveTouch設置爲YES。
注意: boardImage是一個'正常的'UIImageView',這些圖塊與子類一起!

此外:當我設置boardImage的UserInteraction設置爲NO時,ScrollView會縮放但不會記錄touchBegan。如果我將它設置爲YES,ScrollView不會縮放,但touchBegan會被記錄下來。

所以我離開它,所以我可以縮放,但是一旦它們在ScrollView中,它們不再可拖動。
我在做什麼錯?或者我錯過了什麼?

PS:設置delaysContentTouches屬性使滾動無效的,這樣也沒有辦法了......

代碼: RootViewController_iphone.m

@implementation RootViewController_iphone 

@synthesize boardScrollView; 
@synthesize dragObject; 
@synthesize boardImage; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    UIImage *image = [UIImage imageNamed:@"greyblue_numbered_15x15_900x900.png"]; 
    self.boardImage = [[UIImageView alloc] initWithImage:image]; 
    self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; 
    [self.boardScrollView addSubview:self.boardImage]; 

    self.boardImage.userInteractionEnabled = YES; 

    self.boardScrollView.contentSize = image.size; 
    self.boardScrollView.userInteractionEnabled = YES; 
    self.boardScrollView.canCancelContentTouches = NO; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
if ([touches count] == 1) { 
    // one finger 

    CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 

    for (UIImageView *iView in self.view.subviews) { 
     if ([iView isMemberOfClass:[TileImageView class]]) { 
      if (touchPoint.x > iView.frame.origin.x && 
       touchPoint.x < iView.frame.origin.x + iView.frame.size.width && 
       touchPoint.y > iView.frame.origin.y && 
       touchPoint.y < iView.frame.origin.y + iView.frame.size.height) 
      { 
       self.dragObject = iView; 
       self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x, 
               touchPoint.y - iView.frame.origin.y); 
       self.homePosition = CGPointMake(iView.frame.origin.x, 
               iView.frame.origin.y); 
       [self.view bringSubviewToFront:self.dragObject]; 
      } 
     } 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 

    CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x, 
              touchPoint.y - touchOffset.y, 
              self.dragObject.frame.size.width, 
              self.dragObject.frame.size.height); 
    self.dragObject.frame = newDragObjectFrame; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    NSString *tmp = touch.view.description; 

    CGPoint touchPointScreen = [[touches anyObject] locationInView:self.view]; 
    CGPoint touchPointImage = [[touches anyObject] locationInView:self.boardImage]; 
    CGPoint touchPointBoard = [[touches anyObject] locationInView:self.boardScrollView]; 

    if (touchPointScreen.x > self.boardScrollView.frame.origin.x && 
     touchPointScreen.x < self.boardScrollView.frame.origin.x + self.boardScrollView.frame.size.width && 
     touchPointScreen.y > self.boardScrollView.frame.origin.y && 
     touchPointScreen.y < self.boardScrollView.frame.origin.y + self.boardScrollView.frame.size.height) 
     { 
     self.dragObject.frame = CGRectMake(touchPointImage.x - touchOffset.x, 
              touchPointImage.y - touchOffset.y, 
              self.dragObject.frame.size.width, 
              self.dragObject.frame.size.height); 

     [self.boardImage addSubview:self.dragObject]; // add to image 


     } 
} 

代碼: TileImageView.m

@implementation TileImageView: 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.exclusiveTouch = YES; 
    } 
    return self; 
} 
+0

嘗試通過爲'UIImageView'啓用'userinteraction'來嘗試一次。 – Exploring

+0

嗯,在界面生成器中,還有一個Interaction設置...... :(現在父視圖中的觸摸被記錄,當我在de scrollview中觸摸時...但現在拖放不再有效... – RobertvdBerg

+0

有你在子視圖中添加touch委託方法? – Exploring

回答

1

您已經有了子類UIImageView,因此您可以在子類中執行以下操作:
1-覆蓋touchsBegan,didEnd和didMove
2-內這些方法將它們傳遞到其是UIScrollView的上海華:[self.superview touchsBegan .... ]
3-如果不的UIImageView接收這些消息,則在初始化添加self.userInteractionEnabled = YES; 希望這正在進行以幫助您

+0

當我調試時,應用程序不會在子類中輸入touchBegan方法:(即使添加self.UserInteractionEnabled = YES。 – RobertvdBerg

+0

我也認爲問題在於: self.boardScrollView.contentSize = image.size;嘗試做到這一點self.scrollview.clipsToBounds = YES。可能是內容大小非常小,所以滾動視圖沒有得到接觸 –

+0

不,也做沒有解決問題。 – RobertvdBerg