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

发布于 2024-10-12 15:48:13 字数 2095 浏览 8 评论 0原文

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

我已经通过代码创建了滚动视图,并且在名为“arrayofviews”的数组中有子视图。这些子视图与数组中的视图相同。我可以在普通视图上移动来自数据库的这些子视图,但是当我将这些视图添加到滚动视图中,但它不起作用。

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

.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
  }
 }

}

I have craeted a UIScrollView using code and i have created subviews which i am creating dynamically from the DB that are added to the array of views and to the UIScrollviews.I am using touchesBegan and touchesMoved methods.The subview which i am selecting is not recognized,but the control comes into touchesBegan and touches moved method.I have posted the code below.Please anybody help me to overcome from this issue.

I have created the scrollview through code and i have subviews in an array named "arrayofviews".these subviews are the same views which are in the array.I am able to move these subviews which are from the DB on a normal view,but when i added these views to the scrollview its not working.

I have tried so many solutions which are in the net,but i couldn't get a proper solution.

.m file

- (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
  }
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-10-19 15:48:14

更改该行

scrollview.userInteractionEnabled = NO;

scrollview.userInteractionEnabled=YES;

原因

userInteractionEnabled 告诉设备,是否接受触摸事件。当您禁用滚动视图上的触摸时,它如何接收触摸事件......

干杯!

change the line

scrollview.userInteractionEnabled=NO;

to

scrollview.userInteractionEnabled=YES;

reason

userInteractionEnabled tells the device, whether to accept touch events. As you disabled the touch on scrollView then how it could recived touches events...

cheers!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文