iphone - 我想在IOS中画一个类似矩形的形状,带有可拖动的角

标签 iphone objective-c ios xcode ipad

我想在照片上覆盖一个盒子形状,并允许用户选择每个角,然后将角拖动到他们想要的位置。

我可以使用 4 个不可见按钮(代表每个角)来响应拖动事件来获取每个角的 x,y 点,但是 xcode 中是否有一些可用的线条绘制功能,而无需接触任何游戏 api 类?我想我想在 UIView 上画线。

非常感谢, -代码

最佳答案

创建 UIView 的子类来表示您的 View 。将 UIImageView 添加到您的 View 中。这将保存带有用户绘图的图像。

UIView 子类中启用用户交互。

self.userInteractionEnabled = YES;

通过在子类中实现此方法来检测开始点击:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // We are starting to draw
    // Get the current touch.
    UITouch *touch = [touches anyObject];    
    startingPoint = [touch locationInView:self];
}

检测最后一次点击绘制直线:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];    
    endingPoint = [touch locationInView:self];

    // Now draw the line and save to your image
    UIGraphicsBeginImageContext(self.frame.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 10);
    CGContextMoveToPoint(context, NULL, startingPoint.x, startingPoint.y);
    CGContextAddLineToPoint(context, NULL, endingPoint.x, endingPoint.y);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);
    CGContextSetRGBStrokeColor(context, 255, 255, 255, 1);
    CGContextStrokePath(context);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRelease(context);
}

关于iphone - 我想在IOS中画一个类似矩形的形状,带有可拖动的角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11326568/

相关文章:

ios - ReactiveCocoa::我可以在 createsignal 中订阅一个信号吗?

ios - 请求完成处理程序 fatal error : unexpectedly found nil while unwrapping an optional value exception

ios - Firebase 不会报告 iOS 中的所有崩溃,而 Xcode 10 会报告所有这些崩溃,我们能否以某种方式在 firebase 上跟踪所有这些崩溃?

ios - Backendless swift 如何从 MBAAS 存储加载对象

ios - 将文本设置为 uitextview 应用程序崩溃 - ios sdk

iphone - 基于对象值如何获取 NSMutableArray 索引值

iphone - NSArray 的前三项变成 NSString?

iphone - 使用 NavigationBar 禁用 UIScrollView 垂直滚动

ios - 为什么Objective-C中的BOOL类型是char?

objective-c - 解析 NSURL 的重定向?