iOS:UIScroll View - 阻止在视图上滚动

时间:2012-10-04 19:00:52

标签: iphone ios uiview uiscrollview event-handling

我正在尝试回应UIScrollView中的视图触摸。我设法通过子类化UIScrollView将标准触摸转发到我的子视图,但如果我按下视图,然后滚动,然后放开,不会调用“touchesEnded”方法,因此视图仍然突出显示。

我想纠正这种行为。

我试图按照本指南阻止在点击视图时滚动:

http://www.musicalgeometry.com/?p=1404

一步是将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{

}

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

  1. 如何让locPhoneView.frame为我提供正确的尺寸?

  2. 如果失败了,有没有办法确保调用“touchesEnded”方法,即使在触摸的开始和结束之间有滚动?

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么,但看起来像ScrollView委托像scrollViewDidScroll:或scrollViewDidEndDragging:或scrollViewDidEndDecelerating:可能对你有所帮助。请参阅Responding to Scrolling and Dragging

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