UISliderトラックでイベントを取得するには? UISlider のボタンでイベントを取得できますが、順調ではありません。どうすればいいですか?
ありがとう
UISliderトラックでイベントを取得するには? UISlider のボタンでイベントを取得できますが、順調ではありません。どうすればいいですか?
ありがとう
UISlider
これを行うには、 touchesBegan、touchesEnd、touchesCancelled、touchesMoved などの touches イベントをサブクラス化し、実装する必要があります。
@interface yourSlider:UISlider
@end
@implementation yourSlider
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
@end
別の回避策:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
gr.delegate=self;
[Slider addGestureRecognizer:gr];
-(void)sliderTapped:(UIGestureRecognizer*)g{
UISlider* s = (UISlider*)g.view;
if (s.highlighted)
return;
CGPoint pt = [g locationInView: s];
CGFloat value = s.minimumValue + pt.x / s.bounds.size.width * (s.maximumValue - s.minimumValue);
[s setValue:value animated:YES];
}
をサブクラス化し、そのメソッドUISlider
をオーバーライドします。touchesBegan:withEvent:
タッチ イベントからポイント値を取得し、スライダーの幅に対するポイントの .x 値によってパーセンテージを計算します。
答えは正しいですが、ここに回避策があります。スライダーのスライダー調整に等しいクリアボタンを追加し、そのタッチポイントを検出して、X 位置をスライダー値に変換するだけです。
- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
UIView *button = (UIView *)sender;
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint location = [touch locationInView:button];
NSLog(@"Location in button: %f, %f", location.x, location.y); \\ use this x to determine slider's value
}