我已经使用代码创建了一个 UIScrollView,并且我创建了从数据库动态创建的子视图,这些子视图被添加到视图数组和 UIScrollviews。我正在使用 touchesBegan 和 touchesMoved 方法。我选择的子视图无法识别,但是控件进入 touchesBegan 和 touchesmoved 方法。我在下面发布了代码。请任何人帮助我克服这个问题。
我已经通过代码创建了滚动视图,并且我在名为“arrayofviews”的数组中有子视图。这些子视图与数组中的视图相同。我能够将这些来自数据库的子视图移动到普通视图上,但是当我将这些视图添加到滚动视图中,它不起作用。
我已经尝试了很多网络中的解决方案,但我找不到合适的解决方案。
.m 文件
- (void)viewDidAppear:(BOOL)动画
{
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=否;
.
.
.
.
//这里我从数据库中检索控件并添加到“arrayofviews”数组中,这是一个 NSMutableArray
//我已经将这部分的子视图添加到滚动中
[滚动视图添加子视图:vw1];
[滚动视图添加子视图:vw2];
.
.
.
scrollview.userInteractionEnabled=NO;
scrollview.scrollEnabled=假;
[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.x=scrollview.contentOffset.x;
touchLocation.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
vw.userInteractionEnabled=YES;
if([触摸视图]==vw)
{
vw.center=触摸位置;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //触发触摸事件的方法
{
[self.nextResponder touchesMoved:touches withEvent:event];
UITouch *touch = [[事件 touchesForView:scrollview] anyObject];
CGPoint touchLocation1;
touchLocation1.x=scrollview.contentOffset.x;
touchLocation1.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
if([触摸视图]==vw)
{
vw.center=touchLocation1;//移动控件的语句
}
}
}