将手指从按钮滑动到 iPhone 中的另一个按钮

Posted

技术标签:

【中文标题】将手指从按钮滑动到 iPhone 中的另一个按钮【英文标题】:Slide finger from button to another button in iPhone 【发布时间】:2011-03-25 10:42:03 【问题描述】:

我是 iPhone 开发的初学者。我们如何使用手指从一个按钮拖动到另一个按钮?我使用了touchMoved,但它不起作用。有可能吗?

谢谢。

【问题讨论】:

你能提供一些详细的信息吗? 你是不是想实现 iPhone 滑锁之类的东西? 【参考方案1】:

试试这个 -

![- (void)create_Custom_UISlider

    CGRect frame = CGRectMake(10.0, 50.0, 300, 52.0);
    CGRect thumb = CGRectMake(0.0, 0.0, 71.0, 47.0);
    customSlider = [[UISlider alloc] initWithFrame:frame];
    [customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    // in case the parent view draws with a custom color or gradient, use a transparent color
    customSlider.backgroundColor = [UIColor clearColor];    
    UIImage *stetchLeftTrack = [[UIImage imageNamed:@"customTrack.png"]
                                stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    UIImage *stetchRightTrack = [[UIImage imageNamed:@"customTrack.png"]
                                 stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    [customSlider setThumbImage: [UIImage imageNamed:@"customThumb_off.png"] forState:UIControlStateNormal];
    [customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
    [customSlider thumbRectForBounds: thumb trackRect: frame value: customSlider.value];
    customSlider.minimumValue = 0.0;
    customSlider.maximumValue = 1.0;
    customSlider.continuous = YES;
    customSlider.value = 0.0;



- (void) sliderAction: (UISlider *) sender

    if (customSlider.value != 1.0)  //if the value is not the max, slide this bad boy back to zero
    
        [sender setValue: 0 animated: YES];
    
    else   
               if (customSlider.value == 1.0)  //if the value is max, slide this bad boy back to zero and execute code
    
        [sender setValue: 0 animated: NO];
        
                // the rest of your code here.
        
][1]

也看看这个问题 - iPhone "slide to unlock" animation

【讨论】:

我正在尝试做九点图案锁。谁能帮帮我?【参考方案2】:

是的,有可能。首先你把这个方法:.............

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    UITouch *touch = [[event allTouches] anyObject];

btn addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];




and

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event

 scroll.frame=CGRectMake(self.scroll.frame.origin.x, self.scroll.frame.origin.y, self.scroll.frame.size.width+1800, self.scroll.frame.size.height);
 scroll.contentSize=CGSizeMake(2000,1200);
 scroll.backgroundColor=[UIColor clearColor];
 UITouch *touch = [[event touchesForView:button] anyObject];

 NSLog(@"%d",button.frame.origin.x);

 CGPoint previousLocation = [touch previousLocationInView:button];
 CGPoint location = [touch locationInView:button];

 CGFloat delta_x = location.x - previousLocation.x;
 CGFloat delta_y = location.y - previousLocation.y;


  button.center = CGPointMake(button.center.x + delta_x,
         button.center.y + delta_y);


【讨论】:

scroll.frame=CGRectMake(self.scroll.frame.origin.x, self.scroll.frame.origin.y, self.scroll.frame.size.width+1800, self.scroll.框架.尺寸.高度);

以上是关于将手指从按钮滑动到 iPhone 中的另一个按钮的主要内容,如果未能解决你的问题,请参考以下文章

在 3 个按钮上滑动手指 - Xcode 和 Swift

如何在手指从1按钮拖动到下一个按钮时触发UIButton事件

如何在不抬起手指的情况下更改我按下的按钮? (在LibGDX中)

通过在多个按钮上滑动手指来选择多个按钮

无法检测到按钮上的“滑动打开”事件

在屏幕上滑动手指激活按钮,就像它们被按下一样