处理UIScrollView中子视图(uiviews)的触摸问题

时间:2011-01-20 10:48:33

标签: iphone uiscrollview uitouch touches

我使用代码创建了一个UIScrollView,我创建了子视图,我从DB中动态创建了添加到视图数组和UIScrollviews的子视图。我正在使用touchesBegan和touchesMoved方法。我选择的子视图是不被识别的,但是控件进入touchesBegan并触及移动的方法。我已经发布了下面的代码。请任何人帮助我克服这个问题。

我已经通过代码创建了scrollview,并且我在名为“arrayofviews”的数组中有子视图。这些子视图与数组中的视图相同。我能够在普通视图中移动这些来自数据库的子视图,但是当我将这些视图添加到scrollview时它无法正常工作。

我已经尝试了很多网络中的解决方案,但我无法得到合适的解决方案。

.m文件

- (void)viewDidAppear:(BOOL)animated
{

 scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 374)];
 scrollview.backgroundColor=[UIColor clearColor];
 [scrollview setScrollEnabled:YES];
 [scrollview setContentSize:CGSizeMake(300, 600)];
 scrollview.showsVerticalScrollIndicator = YES;
 scrollview.delaysContentTouches=NO;

    .
 .
 .
 .
    //here i am retrieving the controls from the DB and adding into the "arrayofviews" array which is an NSMutableArray

    //I have added subviews in this part to the scroll like
    [scrollview addSubview:vw1];

    [scrollview addSubview:vw2];

 .
 .
 .

 scrollview.userInteractionEnabled=NO;
 scrollview.scrollEnabled=FALSE;
 [self.view addSubview:scrollview];

 [self.view bringSubviewToFront:scrollview];


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 scrollview.userInteractionEnabled=YES;
 [self.nextResponder touchesBegan:touches withEvent:event];
 UITouch *touch = [[event allTouches] anyObject];

 CGPoint touchLocation;
 touchLocation.x=scrollview.contentOffset.x;
 touchLocation.y=scrollview.contentOffset.y;
 for(UIView *vw in arrayOfViews)
 {
  vw.userInteractionEnabled=YES;
  if([touch view]==vw)
  {
   vw.center=touchLocation;
  }
 }

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  //method to intiate the touch events
{
 [self.nextResponder touchesMoved:touches withEvent:event]; 
 UITouch *touch = [[event touchesForView:scrollview] anyObject];
 CGPoint touchLocation1;
 touchLocation1.x=scrollview.contentOffset.x;
 touchLocation1.y=scrollview.contentOffset.y;
 for(UIView *vw in arrayOfViews)
 {
  if([touch view]==vw)
  {
   vw.center=touchLocation1;   //statement to move the control
  }
 }

}

1 个答案:

答案 0 :(得分:1)

更改行

scrollview.userInteractionEnabled = NO;

  

scrollview.userInteractionEnabled = YES;

原因

userInteractionEnabled告诉设备是否接受触摸事件。当您在scrollView上禁用触摸时,它可以如何重新触摸事件......

欢呼声!!!