2013-05-07 60 views
1

我已經找到了教程,我想在我的視圖控制器實現,但代碼寫在UIView所以如何將UIView代碼轉換爲UIViewControlle r它可以在這裏工作是我的代碼如何將UIView代碼轉換爲UIViewController與筆尖在ipad

#import <UIKit/UIKit.h> 


    @interface MyLineDrawingView : UIView { 

    UIBezierPath *myPath; 
    UIColor *brushPattern; 
    } 

    @end 


    #import "MyLineDrawingView.h" 


    @implementation MyLineDrawingView 

    - (id)initWithFrame:(CGRect)frame 
{ 

    self = [super initWithFrame:frame]; 
    if (self) { 
    // Initialization code 

    self.backgroundColor=[UIColor whiteColor]; 
    myPath=[[UIBezierPath alloc]init]; 
    myPath.lineCapStyle=kCGLineCapRound; 
    myPath.miterLimit=0; 
    myPath.lineWidth=10; 
    brushPattern=[UIColor redColor]; 
    } 
    return self; 
    } 

如果您執行自定義繪圖,則只能覆蓋drawRect:。 一個空的實現在動畫過程中會對性能產生不利影響。

- (void)drawRect:(CGRect)rect 
{ 

[brushPattern setStroke]; 
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
// Drawing code 
//[myPath stroke]; 
} 

    #pragma mark - Touch Methods 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
[myPath moveToPoint:[mytouch locationInView:self]]; 

} 

- (無效)touchesMoved:(NSSet中*)觸摸withEvent:方法(的UIEvent *)事件 {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
[myPath addLineToPoint:[mytouch locationInView:self]]; 
[self setNeedsDisplay]; 

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

    { 
     //handle touch event 

     } 
+0

最簡單的方法是將視圖添加爲您的視圖控制器的子視圖...也drawRect這個方法是視圖.... – Venkat 2013-05-07 06:07:32

+0

@MountainLion可以請你告訴我withoud使子視圖如何可能我寫這篇文章的代碼在ViewController中,如果我想使 – 2013-05-07 06:08:58

+0

請參閱此,http://stackoverflow.com/questions/1053761/iphone-why-isnt-drawrect-getting-called/1053776#1053776和http://stackoverflow.com/questions/8235967/how-to-access-drawrect-method-from-uiviewcontroller – Venkat 2013-05-07 06:14:51

回答

3

創建UIViewController對象和設置視圖財產這個觀點。像這樣的東西應該工作。

UIViewController *vc = [[UIViewController alloc] init]; 
MyLineDrawingView *myView = [[MyLineDrawingView alloc] init]; 
vc.view = myView;