2012-05-21 28 views
2

我想在uview上繪製隨機設計,就像我們在畫筆上繪畫一樣,我想要用戶在屏幕上觸摸的地方開始繪製。就好像他想寫出的東西比畫出來的還要好。如果他想製作鴨子或球,那麼他就可以製作出來。 PLZ幫助我。在uiview上繪製隨機設計

+0

可以複製上述問題。請參閱鏈接。 [1]:http://stackoverflow.com/questions/3128752/draw-line-in-uiview – iMash

+0

沒有我的問題是不同的在視圖上繪製任何設計 – Chandni

+0

這可能是最好的啓動目的。 http://www.edumobile.org/iphone/ipad-development/draw-circle-triangle-and-rectangle-in-iphone/ – iMash

回答

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); 

}