简体   繁体   English

将UIImageView / UIView拖到1轴上而没有view.center跳转到触摸位置(从视图上的任何位置拖动以在x轴上移动)

[英]Drag a UIImageView/UIView on 1 axis without having view.center jump to touch location (dragging from anywhere on view to move on x-axis)

I'm creating a draggable UIImageView much like the iPhone/iPod Touch's "Slide to Unlock" slider when the device is locked. 我正在创建可拖动的UIImageView,就像锁定设备时iPhone / iPod Touch的“滑动解锁”滑块一样。

I can get the view to move smoothly along the x-axis by using: float newCenter = touchPoint.x; 我可以使用以下方法使视图沿x轴平滑移动: float newCenter = touchPoint.x;

But this doesn't take into account the offset of the touch from the center. 但是,这没有考虑到触摸相对于中心的偏移。 It's simply moving the center point of the view to the touch point. 只是将视图的中心点移动到接触点。 I'm trying to move the view by dragging it from any point along the x-axis of the UIImageView. 我试图通过沿UIImageView的x轴的任意点拖动视图来移动视图。

Below, I tried calculating the newCenter within TouchesMoved but it fails to slide smoothly. 下面,我尝试在TouchesMoved中计算newCenter,但滑动失败。

The code: 编码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self]; // self is a UIView

    CGRect sliderRect = sliderView.frame; // sliderView is a UIImageView

    if (CGRectContainsPoint(sliderRect, touchPoint)){

        float sliderHalfWidth = sliderView.frame.size.width / 2;
        float leftEdge = sliderView.center.x - sliderHalfWidth;
        float distanceToTouchFromEdge = touchPoint.x - leftEdge;

        float newCenter = (distanceToTouchFromEdge - sliderHalfWidth) + touchPoint.x; // <--- the math here is probably incorrect. I tried a few other formulas, none of which gave me the results I was looking for.
        //float newCenter = touchPoint.x;  <--- This slides smoothly but always centers view on touch-point (we want to drag from anywhere)

        NSLog(@"--------------");
        NSLog(@"sliderHalfWidth: %f", sliderHalfWidth);
        NSLog(@"sliderView.center.x: %f", sliderView.center.x);
        NSLog(@"leftEdge: %f", leftEdge);
        NSLog(@"distanceToTouchFromEdge: %f", distanceToTouchFromEdge);
        NSLog(@"touchPoint.x: %f", touchPoint.x);
        NSLog(@"newCenter: %f", newCenter);
        NSLog(@"--------------");

        sliderView.center = CGPointMake(newCenter, sliderView.frame.size.height/2);

    }
}

Any thoughts? 有什么想法吗?

I ended up using a UISlider. 我最终使用了UISlider。

I found the post below, which is incredibly helpful if you're looking for the same solution. 我在下面找到了该帖子,如果您正在寻找相同的解决方案,这将非常有用。

It has a link to an xcode project with a fairly accurate recreation of Apple's native "Slide to Unlock" slider feature. 它具有一个xcode项目的链接,可以相当准确地重新利用Apple的本机“ Slide to Unlock”滑块功能。

http://www.iphonedevsdk.com/forum/iphone-sdk-development/11470-there-way-i-can-use-slide-unlock-slider.html#post124686 http://www.iphonedevsdk.com/forum/iphone-sdk-development/11470-there-way-i-can-use-slide-unlock-slider.html#post124686

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM