我想在照片上覆盖一个盒子形状,并允许用户选择每个角,然后将角拖动到他们想要的位置。
我可以使用 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/