自定义 UISlider:拖动到外部时避免更新

发布于 2024-12-18 01:30:20 字数 750 浏览 2 评论 0原文

我对 iPhone 开发还很陌生,我正在构建我的第一个应用程序:) 在我的一个视图控制器中,我构建了一个 customSlider,它应该充当本机“滑动解锁”滑块。

我现在的疑问是如何实现“拖到外面”的行为。如前所述,我希望它与本机滑块完全相同,这意味着当手指拖动滑块时,如果它移出滑块,滑块应该为零。

我的疑问不是动画部分(我已经成功使用动画块),而是控制事件部分。我应该使用哪个控制事件?

我正在使用:

[customSlider addTarget:self action:@selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];

处理滑动部分(手指滑动光标),并

[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

处理释放部分,但问题是,如果我在外面释放手指,则不会调用 sliderAction 函数。

编辑: 我试图实现 @Bruno Domingues 给我的解决方案,但我意识到问题是默认情况下 UISlider 会不断更新,即使手指被拖动到它之外(尝试打开例如系统中的亮度部分)首选项,您会看到滑块将继续更新,即使您将其拖动到其外部)。所以我的问题可以重新定义:如何避免这种默认行为并仅当手指在滑块上移动时才更新滑块?

I'm quite new to iPhone development and I'm building my first app :)
In one of my view controllers I built a customSlider that should behave as the native "slide to unlock" slider.

My doubt right now is how to implement the "drag outside" behaviour. As said, I would like to have it exactly as the native slider, that means that when the finger is dragging the slider if it moves out the slider, the slider should be going to zero.

My doubt is not on the animation part (I'm already using animation block successfully), but on control events part. Which control event should I use?

I'm using:

[customSlider addTarget:self action:@selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];

to handle the sliding part (finger sliding the cursor), and

[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];

to handle the release part, but the problem is that if I release the finger outside, the sliderAction function it's not called.

EDIT:
I've tried to implement the solution @Bruno Domingues gave me, but I'm realizing that the issue is that by default UISlider keep getting updated even if the finger is dragged outside of it (try to open for example the Brightness section in System Preferences and you'll see that the slider will keep on updating even if you drag outside of it). So my question could be redefined: How to avoid this default behaviour and have the slider updating only when the finger is moving on it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-12-25 01:30:20

只需中断自定义子类中的触摸方法,然后仅将您想要执行的触摸转发到超类,如下所示:

in .h:

@interface CustomSlider : UISlider
@end

in .m:

#import "CustomSlider.h"
@implementation CustomSlider
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesCancelled:touches withEvent:event];
}
@end

请注意,如果您的手指移回到控制。为了消除这个问题,如果在视图之外接收到触摸,只需设置一个标志,然后在后续触摸方法中检查该标志。

Simply interrupt the touches methods in your custom subclass and only forward the touches you want acted on to the superclass, like so:

in .h:

@interface CustomSlider : UISlider
@end

in .m:

#import "CustomSlider.h"
@implementation CustomSlider
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    if (touchLocation.x < 0 || touchLocation.y<0)return;
    if (touchLocation.x > self.bounds.size.width || touchLocation.y > self.bounds.size.height)return;
    [super touchesCancelled:touches withEvent:event];
}
@end

Please note that this implementation will start updating the control if your finger moves back to the control. To eliminate this, simply set a flag if a touch is received outside of the view then check that flag in subsequent touches methods.

贪恋 2024-12-25 01:30:20

您需要设置一个 [slider addTarget:self action:@selector(eventDragOutside:) forControlEvents:UIControlEventTouchDragOutside];

并添加一个函数

- (IBAction)eventDragOutside:(UISlider *)sender {
发送者值= 0.0f;
}

You need to set a [slider addTarget:self action:@selector(eventDragOutside:) forControlEvents:UIControlEventTouchDragOutside];

And add a function

- (IBAction)eventDragOutside:(UISlider *)sender {
sender.value = 0.0f;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文