• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

objective-c - 识别用户手势的路径

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:21:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在开发一个屏幕上有 9 个 View 的应用程序,我希望用户以他们想要的方式连接 View ,并将他们的序列记录为密码。 但我不知道我应该使用哪个手势识别器。

我应该使用 CMUnistrokeGestureRecognizer 还是几个滑动手势的组合或其他什么? 谢谢。



Best Answer-推荐答案


您可以使用 UIPanGestureRecognizer ,类似:

CGFloat const kMargin = 10;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // create a container view that all of our subviews for which we want to detect touches are:

    CGFloat containerWidth = fmin(self.view.bounds.size.width, self.view.bounds.size.height) - kMargin * 2.0;

    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(kMargin, kMargin, containerWidth, containerWidth)];
    container.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:container];

    // now create all of the subviews, specifying a tag for each; and

    CGFloat cellWidth = (containerWidth - (4.0 * kMargin)) / 3.0;

    for (NSInteger column = 0; column < 3; column++)
    {
        for (NSInteger row = 0; row < 3; row++)
        {
            UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(kMargin + column * (cellWidth + kMargin),
                                                                    kMargin + row    * (cellWidth + kMargin),
                                                                    cellWidth, cellWidth)];
            cell.tag = row * 3 + column;
            cell.backgroundColor = [UIColor lightGrayColor];
            [container addSubview:cell];
        }
    }

    // finally, create the gesture recognizer

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                          actionselector(handlePan];
    [container addGestureRecognizer:pan];
}

- (void)handlePanUIPanGestureRecognizer *)gesture
{
    static NSMutableArray *gesturedSubviews;

    // if we're starting a gesture, initialize our list of subviews that we've gone over

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        gesturedSubviews = [NSMutableArray array];
    }

    // now figure out whether:
    //   (a) are we over a subview; and
    //   (b) is this a different subview than we last were over

    CGPoint location = [gesture locationInView:gesture.view];

    for (UIView *subview in gesture.view.subviews)
    {
        if (CGRectContainsPoint(subview.frame, location))
        {
            if (subview != [gesturedSubviews lastObject])
            {
                [gesturedSubviews addObject:subview];

                // an example of the sort of graphical flourish to give the
                // some visual cue that their going over the subview in question 
                // was recognized

                [UIView animateWithDuration:0.25
                                      delay:0.0
                                    options:UIViewAnimationOptionAutoreverse
                                 animations:^{
                                     subview.alpha = 0.5;
                                 }
                                 completion:^(BOOL finished){
                                     subview.alpha = 1.0;
                                 }];
            }
        }
    }

    // finally, when done, let's just log the subviews
    // you would do whatever you would want here

    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"We went over:");

        for (UIView *subview in gesturedSubviews)
        {
            NSLog(@"  %d", subview.tag);
        }

        // you might as well clean up your static variable when you're done

        gesturedSubviews = nil;
    }
}

显然,您可以按自己的方式创建 subview ,并以任何方式跟踪它们,但我们的想法是让 subview 具有唯一的 tag 编号,而手势识别器只需查看您以一个手势遍历它们的顺序。

即使我没有准确捕捉到您想要的内容,它至少向您展示了如何使用平移手势识别器来跟踪手指从一个 subview 到另一个 subview 的移动。


更新:

如果您想在用户登录时在屏幕上绘制路径,您可以使用 UIBezierPath 创建一个 CAShapeLayer。我将在下面演示,但作为警告,我不得不指出这可能不是一个很好的安全功能:通常输入密码时,你会向用户展示足够多的内容,以便他们确认他们正在做他们想要什么,但还不够,以至于有人可以回头看看整个密码是什么。输入文本密码时,通常 iOS 会暂时显示您按下的最后一个键,但很快会将其转换为星号,这样您就无法看到整个密码。这就是我最初的建议。

但是,如果您真的一心想在用户绘制路径时向他们展示路径,您可以使用类似以下的内容。首先,这需要Quartz 2D .因此将 QuartzCore.framework 添加到您的项目中(参见 Linking to a Library or Framework )。二、导入QuartCore头文件:

#import <QuartzCore/QuartzCore.h>

第三,将 pan 处理程序替换为:

- (void)handlePanUIPanGestureRecognizer *)gesture
{
    static NSMutableArray *gesturedSubviews;
    static UIBezierPath *path = nil;
    static CAShapeLayer *shapeLayer = nil;

    // if we're starting a gesture, initialize our list of subviews that we've gone over

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        gesturedSubviews = [NSMutableArray array];
    }

    // now figure out whether:
    //   (a) are we over a subview; and
    //   (b) is this a different subview than we last were over

    CGPoint location = [gesture locationInView:gesture.view];

    for (UIView *subview in gesture.view.subviews)
    {
        if (!path)
        {
            // if the path hasn't be started, initialize it and the shape layer

            path = [UIBezierPath bezierPath];
            [path moveToPoint:location];
            shapeLayer = [[CAShapeLayer alloc] init];
            shapeLayer.strokeColor = [UIColor redColor].CGColor;
            shapeLayer.fillColor = [UIColor clearColor].CGColor;
            shapeLayer.lineWidth = 2.0;
            [gesture.view.layer addSublayer:shapeLayer];
        }
        else
        {
            // otherwise add this point to the layer's path

            [path addLineToPoint:location];
            shapeLayer.path = path.CGPath;
        }

        if (CGRectContainsPoint(subview.frame, location))
        {
            if (subview != [gesturedSubviews lastObject])
            {
                [gesturedSubviews addObject:subview];

                [UIView animateWithDuration:0.25
                                      delay:0.0
                                    options:UIViewAnimationOptionAutoreverse
                                 animations:^{
                                     subview.alpha = 0.5;
                                 }
                                 completion:^(BOOL finished){
                                     subview.alpha = 1.0;
                                 }];
            }
        }
    }

    // finally, when done, let's just log the subviews
    // you would do whatever you would want here

    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // assuming the tags are numbers between 0 and 9 (inclusive), we can build the password here

        NSMutableString *password = [NSMutableString string];

        for (UIView *subview in gesturedSubviews)
            [password appendFormat"%c", subview.tag + 48];

        NSLog(@"assword = %@", password);

        // clean up our array of gesturedSubviews

        gesturedSubviews = nil;

        // clean up the drawing of the path on the screen the user drew

        [shapeLayer removeFromSuperlayer];
        shapeLayer = nil;
        path = nil;
    }
}

这会产生用户在手势进行时绘制的路径:

path of user's finger

与其显示用户每次手指移动所绘制的路径,不如在 subview 的中心之间画线,例如:

- (void)handlePanUIPanGestureRecognizer *)gesture
{
    static NSMutableArray *gesturedSubviews;
    static UIBezierPath *path = nil;
    static CAShapeLayer *shapeLayer = nil;

    // if we're starting a gesture, initialize our list of subviews that we've gone over

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        gesturedSubviews = [NSMutableArray array];
    }

    // now figure out whether:
    //   (a) are we over a subview; and
    //   (b) is this a different subview than we last were over

    CGPoint location = [gesture locationInView:gesture.view];

    for (UIView *subview in gesture.view.subviews)
    {
        if (CGRectContainsPoint(subview.frame, location))
        {
            if (subview != [gesturedSubviews lastObject])
            {
                [gesturedSubviews addObject:subview];

                if (!path)
                {
                    // if the path hasn't be started, initialize it and the shape layer

                    path = [UIBezierPath bezierPath];
                    [path moveToPoint:subview.center];
                    shapeLayer = [[CAShapeLayer alloc] init];
                    shapeLayer.strokeColor = [UIColor redColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    [gesture.view.layer addSublayer:shapeLayer];
                }
                else
                {
                    // otherwise add this point to the layer's path

                    [path addLineToPoint:subview.center];
                    shapeLayer.path = path.CGPath;
                }

                [UIView animateWithDuration:0.25
                                      delay:0.0
                                    options:UIViewAnimationOptionAutoreverse
                                 animations:^{
                                     subview.alpha = 0.5;
                                 }
                                 completion:^(BOOL finished){
                                     subview.alpha = 1.0;
                                 }];
            }
        }
    }

    // finally, when done, let's just log the subviews
    // you would do whatever you would want here

    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // assuming the tags are numbers between 0 and 9 (inclusive), we can build the password here

        NSMutableString *password = [NSMutableString string];

        for (UIView *subview in gesturedSubviews)
            [password appendFormat"%c", subview.tag + 48];

        NSLog(@"assword = %@", password);

        // clean up our array of gesturedSubviews

        gesturedSubviews = nil;

        // clean up the drawing of the path on the screen the user drew

        [shapeLayer removeFromSuperlayer];
        shapeLayer = nil;
        path = nil;
    }
}

这会产生如下内容:

user path with lines

您有各种各样的选择,但希望您现在拥有构建模块,以便您可以设计自己的解决方案。

关于objective-c - 识别用户手势的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161405/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap