3

UISliderトラックでイベントを取得するには? UISlider のボタンでイベントを取得できますが、順調ではありません。どうすればいいですか?

ありがとう

4

4 に答える 4

4

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
于 2012-12-17T13:01:00.657 に答える
1

別の回避策:

    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];
}
于 2013-01-06T13:53:52.200 に答える
1

をサブクラス化し、そのメソッドUISliderをオーバーライドします。touchesBegan:withEvent:タッチ イベントからポイント値を取得し、スライダーの幅に対するポイントの .x 値によってパーセンテージを計算します。

于 2012-12-17T13:00:41.307 に答える
1

答えは正しいですが、ここに回避策があります。スライダーのスライダー調整に等しいクリアボタンを追加し、そのタッチポイントを検出して、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

      }
于 2013-01-06T11:25:37.713 に答える