如何在objective-c中按照您的手指制作图像? [英] How do you make an image follow your finger in objective-c?
问题描述
如何在objetive-c中按照您的手指制作图像?
How do you make an image follow your finger in objetive-c?
推荐答案
- 创建你的 UIView 并配置它(或其任何子类,如 UIImageView )
- 将图片的位置设置为用户触摸的位置:
- Create your UIView and configure it (or any subclass thereof such as a UIImageView)
- Set the position of your Image to be where the user is touching:
有四个委托方法用于接受触摸事件,这些方法是从 UIResponder ,例如 UIView 。使用最适合您的委托方法。如果你想要它跟随你的手指,这将是 -touchesMoved :
There are four delegate methods for accepting touch events which are a part of any class that inherits from UIResponder such as a UIView. Use the delegate method that is most appropriate to you. If you want it to follow your finger, this will be -touchesMoved:
- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self];
myImageView.center = pt;
}
您可以使用的其他委托方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
我写了一个示例应用程序,它可以完全满足您的需求。这是一个绘制石英2D图形的演示,但它会绘制一个红色正方形和黑色圆圈,您可以拖动手指,并且应该足够简单:
I wrote an example application that does exactly what you want. It's a demo of drawing Quartz 2D graphics, but it draws a red square and black circle where you drag your finger and should be simple enough to follow:
替代文字http://brockwoolf.com/shares/stackoverflow/3445494/screen.png
下载Xcode项目( 32kb)
这篇关于如何在objective-c中按照您的手指制作图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!