Iphone iOS:UIScroll View-禁止在查看时滚动触摸

Iphone iOS:UIScroll View-禁止在查看时滚动触摸,iphone,ios,uiview,uiscrollview,event-handling,Iphone,Ios,Uiview,Uiscrollview,Event Handling,我试图在UIScrollView中回应对视图的触摸。我通过对UIScrollView进行子类化,成功地将标准触摸转发到我的子视图,但如果我按下视图,然后滚动,然后放开,则不会调用“touchesEnded”方法,因此视图仍然高亮显示 我想纠正这种行为 我试图遵循此指南,以防止在点击视图时滚动: 其中一个步骤涉及将CGRect属性设置为视图的框架。当我尝试这样做时,它说我的子视图的帧是(0,0,0,0)。这是在-viewDidLoad中,因此该视图应该已经加载了 -viewDidLoad: -

我试图在UIScrollView中回应对视图的触摸。我通过对UIScrollView进行子类化,成功地将标准触摸转发到我的子视图,但如果我按下视图,然后滚动,然后放开,则不会调用“touchesEnded”方法,因此视图仍然高亮显示

我想纠正这种行为

我试图遵循此指南,以防止在点击视图时滚动:

其中一个步骤涉及将CGRect属性设置为视图的框架。当我尝试这样做时,它说我的子视图的帧是(0,0,0,0)。这是在-viewDidLoad中,因此该视图应该已经加载了

-viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [scrollView setScrollEnabled:YES];
    [scrollView setDelaysContentTouches:NO];

    //Here I attempt to get the frame of my subview, but it gives me
    //(0,0,0,0)
    scrollView.subViewRect=locPhoneView.frame;

    locImageView.image=locImage;
    locNameLabel.text=locName;
    locAddressLabel.text=locAddress;
    locPhoneLabel.text=locPhone;
    monHoursLabel.text=[NSString stringWithFormat:@"Mon:     %@",[locHours objectAtIndex:0]];
    tueHoursLabel.text=[NSString stringWithFormat:@"Tue:      %@",[locHours objectAtIndex:1]];
    wedHoursLabel.text=[NSString stringWithFormat:@"Wed:     %@",[locHours objectAtIndex:2]];
    thuHoursLabel.text=[NSString stringWithFormat:@"Thu:      %@",[locHours objectAtIndex:3]];
    friHoursLabel.text=[NSString stringWithFormat:@"Fri:        %@",[locHours objectAtIndex:4]];
    satHoursLabel.text=[NSString stringWithFormat:@"Sat:       %@",[locHours objectAtIndex:5]];
    sunHoursLabel.text=[NSString stringWithFormat:@"Sun:      %@",[locHours objectAtIndex:6]];
    closedSundayLabel.text=[locHours objectAtIndex:7];
}
以下是我的“接触”方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if(touch.view==locPhoneView){
        locPhoneView.backgroundColor=[ColorUtility colorWithHexString:@"83d3f0"];
    }
    if(touch.view==locAddressView){
        locAddressView.backgroundColor=[ColorUtility colorWithHexString:@"83d3f0"];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if(touch.view==locPhoneView){
        locPhoneView.backgroundColor=[UIColor whiteColor];
    }
    if(touch.view==locAddressView){
        locAddressView.backgroundColor=[UIColor whiteColor];
    }

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

}
所以我想我在寻找两个可能的答案:

  • 如何获取locPhoneView.frame以提供正确的尺寸

  • 否则,是否有办法确保调用“touchesend”方法,即使在触摸的开始和结束之间有一个滚动条


  • 我不确定您的问题到底是什么,但看起来像scrollViewDidScroll:或ScrollViewDidEndDraging:或ScrollViewDidEndDecelling:这样的ScrollView代理可能会对您有所帮助。看


    基本上,为了在scrollview中处理触摸,您需要将触摸重定向到下一个响应者,这是您的视图控制器。

    是的,我正在成功地将触摸重定向到下一个响应者。但是,如果您触摸视图,然后滚动,然后释放,它的行为将不正确。在这种情况下不调用“touchesend”。我正在设法解决这个问题。我在上面的链接中找到了一个建议的解决方案,但是我没有得到locPhoneView.frame的正确值。正是在这种情况下,您需要使用scrollViewdidScroll或scrollview委托方法之一。当您触摸视图时,设置标志isTouchBegined,如果您在scrollview委托方法中看到此标志设置,则必须获得endedTouch事件,这意味着您触摸了视图,然后滚动!希望这是有道理的。