矩形将被视为两个点,第一个点将是 touchBegan 点,touchMove 将是第二个点,矩形将根据用户手指移动动态绘制,(就像您在桌面上单击时并移动鼠标,您将获得动态矩形)。
谢谢
最佳答案
好的,下面是如何在 touchesMoved
中绘制矩形(使用 @Nekto touchesBegan
来存储矩形的起点)。
假设您对正在绘制的 UIView 保留一个引用,并将其命名为 drawnView
。
在 touchesBegan
中,我们为 drawnView
这是touchesMoved
:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event
{
// We only take care of a single touch
if ([touches count] == 1)
{
UITouch *touch = [touches anyObject];
// Find the width and height of the rect
CGRect drawnFrame = drawnView.frame
drawnFrame.size.width = [touch locationInView:parentView].x - drawnFrame.origin.x
drawnFrame.size.height = [touch locationInView:parentView].y - drawnFrame.origin.y
[drawnView setFrame:drawnFrame];
}
}
不要复制粘贴那段代码,我不是用 Xcode 或其他任何东西写的。它可能有错误(当然)。但我希望它可以帮助您找到解决方案。请注意,如果您将手指拖到顶部或左侧(高度和宽度为负值),我无法判断它会如何表现。
关于objective-c - 如何根据 iOS 上的 touchMove 事件在屏幕上绘制动态矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047020/