使用 UIPinchGestureRecognizer 绘制线条

2024-01-06

我想使用 UIPinchGeustureRecognizer 画线,我已经尝试了所有 stackoverflow 解决方案,但没有运气。请帮我解决这个问题。我收到以下错误

首先我想知道我的代码逻辑是否正确。并且我没有从触摸开始/触摸移动中得到点。我从中得到两点(void)handleLinePinch:(UIPinchGestureRecognizer *)手势 only.

//My instances in .h file

CGPoint location1,location2;
LineView* l;

- (void)viewDidLoad
{
   [super viewDidLoad];
   l = [[LineView alloc]initWithFrame:self.view.frame];
   [self.view addSubview:l];
   UIPinchGestureRecognizer *linepinch = [[UIPinchGestureRecognizer alloc]    
   initWithTarget:l action:@selector(handleLinePinch:)];
   [l addGestureRecognizer:linepinch];
}


- (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
{
    NSUInteger num_touches = [gesture numberOfTouches];

    // save locations to some instance variables, like `CGPoint location1, location2;`
    if (num_touches >= 1) {
       location1 = [gesture locationOfTouch:0 inView:l];
    }
    if (num_touches >= 2) {
       location2 = [gesture locationOfTouch:1 inView:l];
    }
    [l drawRect:location1 Loc2:location2];
    [l setNeedsDisplay];

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


LineView.m

- (void)drawRect:(CGPoint)location1 Loc2:(CGPoint)location2 {

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, location1.x, location1.y);
CGContextAddLineToPoint(context, location2.x, location2.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}

您必须子类化UIView并覆盖drawRect:方法,将CGContextRef你得到UIGraphicsGetCurrentContext无效于drawRect:方法,并且不要建立对图形上下文的强引用,因为它可能会在对 drawRect: 方法的调用之间发生变化。

当您识别出捏合手势时,将 CGPoint 传递给视图并发送setNeedsDisplay的方法。

总是使用setNeedsDisplay要刷新视图,请勿发送drawRect:直接地。

线视图.m

- (void)drawRect:(CGRect)rect
{
    // p1 and p2 should be set before call `setNeedsDisplay` method
    [self drawRect:location1 Loc2:location2] ;
}

- (void)drawRect:(CGPoint)location1 Loc2:(CGPoint)location2 {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 5.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);
    CGContextMoveToPoint(context, location1.x, location1.y);
    CGContextAddLineToPoint(context, location2.x, location2.y);
    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}

编辑:我假设你只有在两根手指放在一起时才画线。

- (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
{
    NSUInteger num_touches = [gesture numberOfTouches];

    // save locations to some instance variables, like `CGPoint location1, location2;`
    if (num_touches == 2) {
       location1 = [gesture locationOfTouch:0 inView:l];
       location2 = [gesture locationOfTouch:1 inView:l];
    }
    // you need save location1 and location2 to `l` and refresh `l`.
    // for example: l.location1 = location1; l.location2 = location2;
    [l setNeedsDisplay];

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 UIPinchGestureRecognizer 绘制线条 的相关文章

  • Objective-C 使用字符串池吗?

    我知道Java https stackoverflow com questions 3801343 what is string pool in java and C http msdn microsoft com en us librar
  • 比较 Objective C 中的 NSNumber

    我是 Objective C 的初学者 对这种情况有点困惑 我有以下代码 if number1 lt number2 NSLog THE FOLLOWING NUMBER NSLog number1 NSLog IS LESS THAN N
  • 在 Swift 中使用 commitEditingStyle 动态删除 UITable 部分

    我正在处理一个无法解决的问题 我有一个来自客户数据库数组的名称表 每个客户在其他数据成员中都有一个名称属性 我可以成功删除某个部分中的行 但我不能删除该部分 当该部分中的最后一行被删除时 该部分必须消失 I got NSInternalIn
  • 如何将字符串从 Applescript 传递到 Objective C

    我正在开发一个应用程序 我需要能够传递一个字符串变量 from 苹果脚本 to 目标C 我已经弄清楚如何从 Objective C 类中的方法运行 Applescript 但我需要能够将 NSString 设置为 Applescript 中
  • 为什么没有收到(UDP 多播)数据包?

    所以 我一直试图弄清楚为什么这不起作用 但我没有任何线索 我已经成功地从 iPhone 发送数据包并在 Mac 上接收它们 根据 tcpdump 我的 mac 正确发送数据包 此外 如果我在模拟器中运行它 它工作得很好 这让我相信这是一个网
  • Excel VBA 导出到文本文件。需要删除空行

    我有一个工作簿 使用以下脚本将其导出到文本文件 它工作正常 但是当我打开文本文件时 末尾总是有一个空行 这导致我在生成此文本文件后运行的另一个脚本出现问题 有关如何从导出中删除空行的任何帮助 Code Sub Rectangle1 Clic
  • 使用 Cocoa/OSX 合并/堆叠两个图像

    我有一个 CGImageRef 我们称之为原始图像 和一个透明 png 水印 我正在尝试编写一种方法将水印放置在原始内容之上 并返回 CGImageRef 在 iOS 中 我会使用 UIKit 将它们绘制到上下文中 但这在 OSX 中似乎不
  • 将捕获的图像精确裁剪为 AVCaptureVideoPreviewLayer 中的外观

    我有一个使用 AV Foundation 的照片应用程序 我使用 AVCaptureVideoPreviewLayer 设置了一个预览层 它占据了屏幕的上半部分 因此 当用户尝试拍照时 他们只能看到屏幕上半部分看到的内容 这很好用 但是当用
  • 由于 2.23 导致 iOS 应用程序被拒绝 - iOS 数据存储指南

    以下是 Apple 关于拒绝的消息 2 23 应用程序必须遵循 iOS 数据存储指南 否则将被拒绝 2 23 详情 在启动和内容下载时 您的应用程序会存储 6 5 MB 这并不意味着 遵守 iOS 数据存储指南 下一步 请验证只有用户使用您
  • 为具有多个目标和不同平台的项目编写 Podfile

    我正在准备一个支持 OS X 和 iOS 的 Pod 我的 pod 有一些自己的依赖项 这些依赖项在 podspec 文件中定义 因此我使用 Podfile 来管理我用来开发 pod 和运行测试的项目的依赖项 我正在使用 CocoaPods
  • 使用 ZBarSDK 时 iPhone 相机失去自动对焦功能

    我正在开发一个应用程序 用户可以选择是否要扫描条形码或拍摄某物的照片 为了拍照 我正在使用UIImagePickerController照常 为了扫描条形码 我使用 ZbarSDK 1 2ZBarReaderViewController 拍
  • 子类 PFObject 上的 PFUser 属性

    我使用以下类 动态属性以及 m 文件中的 load 和 parseClassName 方法 对 PFObject 进行了子类化 interface DAOpponents PFObject
  • [WebCoreSharedBufferData getBytes:range:]: 范围 {0, 8} 超出数据长度 0'

    我正在更新我的 iOs 7 应用程序 我为 cordova 电话间隙 开发了这个应用程序 当我运行这个应用程序时 我收到错误 FirstDemo 175
  • 在成为FirstResponder或resignFirstResponder的情况下将对象保持在键盘顶部?

    我目前在键盘顶部有一个 UITextField 当您点击它时 它应该粘在键盘顶部并平滑地向上移动 我不知道键盘的具体时长和动画类型 所以确实很坎坷 这是我所拥有的 theTextView resignFirstResponder UIVie
  • 在故事板中将 UITableView 的 rowHeight 设置为 UITableViewAutomaticDimension ?

    在 Xcode 6 中创建 iOS 8 应用程序时 如何设置 UITableViewrowHeight to UITableViewAutomaticDimension In WWDC 2014 第 226 场会议 表和集合视图中的新增功能
  • 如何解决malloc_error_break?

    我在 iOS 3 0 模拟器上遇到此错误 但在 3 1 3 和 3 2 模拟器上没有遇到此错误 创建符号断点后malloc error break 我在日志中看到了这一点 Session started at 2010 02 13 19 1
  • AVCaptureDevice 找不到任何设备

    这行代码是我今天遇到的问题 macOS 应用程序 NSArray devices AVCaptureDevice devicesWithMediaType AVMediaTypeVideo 我更新Xcode后 系统总是让我空着devices
  • 添加/删除带有动画的 UITableViewCell?

    我知道这听起来像是一个愚蠢的问题 但我到处都看过 我怎样才能做到这一点 我知道如何使用 swype to delete 方法来执行此操作 但是我如何在该函数之外执行此操作 请发布一些代码示例 Thanks Coulton self tabl
  • 将 UIButton 中的图像缩放到 AspectFit?

    我想将图像添加到 UIButton 并且还想缩放图像以适合 UIButton 使图像变小 请告诉我该怎么做 这是我尝试过的 但它不起作用 将图像添加到按钮并使用setContentMode self itemImageButton setI
  • ios 导航 堆栈操作

    我在尝试从 iOS 应用程序操作导航堆栈时遇到问题 或者至少是由于这种操纵而产生的行为 我的情况 我有 3 个 ViewController 控制器a显示多个级别 控制器 b 是游戏视图 控制器 c 是某种分数 显然 我将在控制器 a 中选

随机推荐