2014-02-10 30 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