English 中文(简体)
iOS 5 - 触碰时绘制圆形
原标题:iOS 5 - Draw a circle on touch

我想用我的应用程序来画一个圆圈, 以触摸坐标为中心。

这里的代码我现在, 它已经搞砸了, 我还没有在画东西和与上下文合作方面做任何实践。 我在这里错过了什么? 事先谢谢。

查看控制器是 UIViewC主计长的子类

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidUnload
{
[super viewDidUnload];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];

[self doSomething];

//[self.view setNeedsDisplay];

}

- (void) drawCircleAtPoint:(CGPoint)point withRadius:(CGFloat)radius inContext:(CGContextRef)context{
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextAddArc(context, point.x, point.y, radius, 0, 2*M_PI, YES);
CGContextFillPath(context);
UIGraphicsPopContext();
}

- (void)drawRect:(CGRect)rect
{
CGPoint point;
CGContextRef context = UIGraphicsGetCurrentContext();
point.x = self.view.bounds.origin.x + self.view.bounds.size.width/2;
point.y = self.view.bounds.origin.y + self.view.bounds.size.height/2;
CGContextSetLineWidth(context, 5.0);
[[UIColor whiteColor] setFill];
[self drawCircleAtPoint:point withRadius:50 inContext:context];

}

@end

我在这里想要它至少画出屏幕中央的圆圈, 但我甚至不能这样做。

最佳回答

drawRect is a method of UIView class. It won t be available in a ViewController and hence wont be called.

ideally what you should do is to place the drawRect and drawCircleAtPoint in a UIView subclass. Also add the touches Methods in that view class. You can call it, DrawCircleView or something.

现在创建此自定义视图的对象, 并将其添加为子视图, 添加到查看控制器视图中, 查看过 Load 的视图中 。

调用 secNeeds Display 在触摸方法中, 并在 CGPoint 实例变量中存储 touvh 点, 并在绘图矩形中访问它 。

<强> UPDATE

您可能想要保存先前绘制的圆圈 。

有许多可能性, 选择适合你的最佳选择, 我还没有给你详细解释 每一个,但足够的信息 让你去。

  • If you want circles to be editable then you will have to continue adding the circle info regarding the center and radius as one object in an NSMutableArray. and in the beginning of your drawRect you will have to redraw all of them in a loop.

    Infact I would suggest you to use CGPath and keep adding circles to that path and add the path to cgcontext in drawRect keep CGPath as an instance var so that it retains previous information from last drawRect call. But again If you want to be able to have circles drawn with different color then they will have to be in different CGPaths then you can add these CGPath with color info in an object into an mutable Array again.

  • saving and redrawing all those circles will hinder the performance if the array keeps growing. If you dont want undo capabilities or you dont want to edit previous circles. Then you can save whatever is drawn in the end of drawRect as imageData and then keep it as an instance var and redraw it every-time drawRect is called and then draw a circle on top of it and save image again into the same instance.

For example: at the end of drawRect you can save image of your screen into instance-variable _savedImage.

  _savedImage = UIGraphicsGetImageFromCurrentImageContext(); 

在绘图矩形开始重新绘制时, 如果 < saveedImaage > < strong > 并非零 , 那么,

 [_savedImage drawInRect:rect];

then you can draw the circle around the point you tapped on. Also If you want then you can save the drawing into actual image file anytime you want with:

 NSData *imageData = UIImagePNGRepresentation(_savedImage);
[imageData writeToFile:@"imageName.png"];

  • If you want you can explore CGContextSaveGState() and CGContextRestoreGState() They are used to push and pop context to stack respectively. So you can get last context and redraw it and in between restoring and saving you can draw your circle. Checkout this stackOverflow post on the same: Saving and restoring CGContext
问题回答

绘图矩称的方法在 < 强度 > UIView 中发生,而不是在 < 强度 > UIViewcurrent -- -- UIView Control管理一个视图,并自行处理授权和其他相关项目。绘图在视图管理员视图属性中发生。

在您的情况中,您将希望采取以下行动:

  1. Subclass UIView with your own view, with the drawRect and drawCircle methods implemented there
  2. Replace the default UIView in the XIB with your own class (IB -> Identity Inspector -> Custom Class)
  3. Hook up events to detect finger touches and then force it to refresh using [view setNeedsDisplay]

注:亚分类和压倒 UIIMageView s 绘图矩形无济于事 - 它被专门处理 :

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签