我想在uview上繪製隨機設計,就像我們在畫筆上繪畫一樣,我想要用戶在屏幕上觸摸的地方開始繪製。就好像他想寫出的東西比畫出來的還要好。如果他想製作鴨子或球,那麼他就可以製作出來。 PLZ幫助我。在uiview上繪製隨機設計
2
A
回答
0
試試這個。 在這裏,您將不得不使用self.view.frame
無論我有myPic.Frame
。
在頭文件:
#import <Foundation/Foundation.h>
@interface DrawView : UIView {
UIImage *myPic;
NSMutableArray *myDrawing;
}
-(void)drawPic:(UIImage *)thisPic;
-(void)cancelDrawing;
@end
,並實現文件:
#import "DrawView.h"
@implementation DrawView
-(void)drawPic:(UIImage *)thisPic {
myPic = thisPic;
[myPic retain];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
float newHeight;
float newWidth;
if (!myDrawing) {
myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (myPic != NULL) {
float ratio = myPic.size.height/460;
if (myPic.size.width/320 > ratio) {
ratio = myPic.size.width/320;
}
newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;
[myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
}
if ([myDrawing count] > 0) {
CGContextSetLineWidth(ctx, 5);
for (int i = 0 ; i < [myDrawing count] ; i++) {
NSArray *thisArray = [myDrawing objectAtIndex:i];
if ([thisArray count] > 2) {
float thisX = [[thisArray objectAtIndex:0] floatValue];
float thisY = [[thisArray objectAtIndex:1] floatValue];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, thisX, thisY);
for (int j = 2; j < [thisArray count] ; j+=2) {
thisX = [[thisArray objectAtIndex:j] floatValue];
thisY = [[thisArray objectAtIndex:j+1] floatValue];
CGContextAddLineToPoint(ctx, thisX,thisY);
}
CGContextStrokePath(ctx);
}
}
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
[self setNeedsDisplay];
}
-(void)cancelDrawing {
[myDrawing removeAllObjects];
[self setNeedsDisplay];
}
- (void)dealloc {
[super dealloc];
[myPic release];
[myDrawing release];
}
@end
+0
謝謝你的givemetime,但沒有什麼是在模擬器上顯示 – Chandni
0
你必須讓你的UIView以上的子類。在您的控制器class.m文件中,您必須添加並調用這兩個方法。 我希望這可以工作。
- (IBAction爲)明確{
[self.view cancelDrawing];
}
- (IBAction爲)saveDrawing {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(finishedPic, self, @selector(exitProg:didFinishSavingWithError:contextInfo:), nil);
}
相關問題
- 1. 繪製隨機點
- 2. 在隨機地圖中隨機繪製隨機圈子Android
- 3. UIView上的圖像繪製
- 4. 繪製一個隨機值
- 5. 繪製隨機圖像libgdx
- 6. Java:繪製「隨機」星星
- 7. 繪製隨機圓圈
- 8. 繪製隨機厚度線
- 9. CCSpriteSheet隨機崩潰繪製
- 10. UIView動畫隨機
- 11. 在UIView上繪製覆蓋圖
- 12. UIImage在UIView上繪製時反轉
- 13. 基於CGPoint在屏幕上繪製UIView
- 14. 如何在UIView上繪製點?
- 15. 如何在UIView上繪製網格
- 16. 在UIView上快速繪製UIImage圖像
- 17. 在代碼生成UIView上繪製UIBezierPath
- 18. 在Python中繪製3D隨機遊動
- 19. glDrawArrays在原點繪製隨機點?
- 20. Android:在路徑中隨機繪製框
- 21. 在JApplet中繪製隨機點
- 22. 如何繪製隨機飛機
- 23. 如何在設定範圍內的隨機點繪製橢圓?
- 24. 在熱點上隨機繪圖
- 25. 在UIView中繪製圖形
- 26. 在UIView之外繪製
- 27. 試圖在UIView中繪製
- 28. C#XNA - 隨機在頂部X軸上繪製紋理
- 29. 如何在java中隨機繪製的圖像上添加mouselistener?
- 30. 在畫布上繪製隨機彩色圓圈
可以複製上述問題。請參閱鏈接。 [1]:http://stackoverflow.com/questions/3128752/draw-line-in-uiview – iMash
沒有我的問題是不同的在視圖上繪製任何設計 – Chandni
這可能是最好的啓動目的。 http://www.edumobile.org/iphone/ipad-development/draw-circle-triangle-and-rectangle-in-iphone/ – iMash