我正在尝试响应对 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{
}
所以我想我正在寻找两个可能的答案:
如何让 locPhoneView.frame 给我正确的尺寸?
如果做不到这一点,有没有办法确保调用“touchesEnded”方法,即使在触摸的开始和结束之间有滚动?