0

我正在尝试响应对 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”方法,即使在触摸的开始和结束之间有滚动?

4

1 回答 1

0

我不确定你的问题到底是什么,但看起来像 scrollViewDidScroll: 或 scrollViewDidEndDragging: 或 scrollViewDidEndDecelerating: 这样的 ScrollView 代表可能会对你有所帮助。请参阅响应滚动和拖动

基本上要处理滚动视图内的触摸,您需要将触摸重定向到下一个响应者,即您的视图控制器。

于 2012-10-04T19:41:50.583 回答