UIScrollViewのビューのタッチに応答しようとしています。UIScrollViewをサブクラス化することで、標準のタッチをサブビューに転送することができましたが、ビューを押し下げてからスクロールして放すと、「touchesEnded」メソッドが呼び出されないため、ビューは強調表示されたままになります。
この動作を修正したいと思います。
ビューがタップされたときにスクロールしないように、このガイドに従おうとしました。
http://www.musicalgeometry.com/?p=1404
1つのステップでは、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{
}
だから私は2つの可能な答えを探していると思います:
locPhoneView.frameを取得して正しいサイズを指定するにはどうすればよいですか?
それができない場合、タッチの開始と終了の間にスクロールがある場合でも、「touchesEnded」メソッドが確実に呼び出されるようにする方法はありますか?