亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

分享一個iOS下實現(xiàn)基本繪畫板功能的簡單方法

 更新時間:2015年10月30日 10:07:30   作者:念茜  
這篇文章主要介紹了iOS下實現(xiàn)基本繪畫板功能的簡單方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

代碼部分
TouchView.h

復(fù)制代碼 代碼如下:

#import <UIKit/UIKit.h> 
 
@interface TouchView : UIView 

    NSMutableArray *points; 
    NSArray *points_all; 
    CGContextRef context; 
    UIColor *paint_clr; 

@property (strong,nonatomic) NSMutableArray *points; 
@property (strong,nonatomic) NSArray *points_all; 
@property (strong,nonatomic) UIColor *paint_clr; 
 
@end 

TouchView.m

復(fù)制代碼 代碼如下:

#import "TouchView.h" 
 
@implementation TouchView 
@synthesize points, points_all, paint_clr; 
 
- (id)initWithFrame:(CGRect)frame 

    self = [super initWithFrame:frame]; 
    if (self) { 
        // Initialization code 
        paint_clr = [UIColor greenColor]; 
    } 
    return self; 

 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 

    // Drawing code 
    if ((!self.points) || (self.points.count < 2)) { 
        return; 
    } 
       
    context = UIGraphicsGetCurrentContext(); 
    //設(shè)置畫筆粗細(xì)  
    CGContextSetLineWidth(context, 5.0f); 
    //設(shè)置畫筆顏色 
    //[[UIColor blueColor]set ]; 
    // [paint_clr set]; 
    //CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]); 
    CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]); 
     
    //畫以前的軌跡 
    for (int j = 0 ; j < [self.points_all count]; j++) { 
        NSMutableArray *points_tmp = [points_all objectAtIndex:j]; 
             
            for (int i = 0;i < [points_tmp count]-1;i++) 
            { 
                CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue]; 
                CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue]; 
                CGContextMoveToPoint(context, point1.x, point1.y); 
                CGContextAddLineToPoint(context, point2.x, point2.y); 
                CGContextStrokePath(context); 
            } 
        } 
     
    //畫這次 
    for (int i=0; i < [self.points count]-1; i++) { 
        CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue]; 
        CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue]; 
        CGContextMoveToPoint(context, point1.x, point1.y); 
        CGContextAddLineToPoint(context, point2.x, point2.y); 
        CGContextStrokePath(context); 
    }     

 
//不支持多點觸摸 
- (BOOL) isMultipleTouchEnabled 

    return NO; 

 
//創(chuàng)建一個array,并且記錄初始ponit 
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event 

    self.points = [NSMutableArray array]; 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    [self.points addObject:[NSValue valueWithCGPoint:pt]]; 

 
//移動過程中記錄這些points 
//調(diào)用setNeedsDisplay,會觸發(fā)drawRect方法的調(diào)用 
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

    CGPoint pt = [[touches anyObject] locationInView:self]; 
    [self.points addObject:[NSValue valueWithCGPoint:pt]]; 
    [self setNeedsDisplay]; 

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

    NSMutableArray *points_tmp = [[NSMutableArray alloc] initWithArray:self.points]; 
    if (self.points_all == nil) { 
        self.points_all = [[NSArray alloc] initWithObjects:points_tmp, nil]; 
    }else { 
        self.points_all = [self.points_all arrayByAddingObject:points_tmp]; 
    } 

@end 

ViewController.h

復(fù)制代碼 代碼如下:

#import <UIKit/UIKit.h> 
 
@class TouchView; 
@interface ViewController : UIViewController 

    TouchView *tv; 

@end 

ViewController.m

復(fù)制代碼 代碼如下:

#import "ViewController.h" 
#import "TouchView.h" 
 
@interface ViewController () 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.userInteractionEnabled = YES; 
     
  // TouchView *tv = [[TouchView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 260.0f)]; 
    tv = [[TouchView alloc]initWithFrame:self.view.frame]; 
    tv.backgroundColor = [UIColor blackColor]; 
     
    [self.view addSubview:tv]; 
     
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[@"White Red Blue Green Yellow" componentsSeparatedByString:@" "]]; 
    seg.segmentedControlStyle = UISegmentedControlSegmentCenter; 
    seg.tintColor = [UIColor blackColor];  
    seg.center = CGPointMake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));  
    [self.view addSubview:seg]; 
     
    [seg addTarget:self action:@selector(colorChange:) forControlEvents:UIControlEventValueChanged]; 

 
- (void)viewDidUnload 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
- (void) colorChange: (UISegmentedControl *) seg 

    switch ([seg selectedSegmentIndex]) 
    { 
        case 0:  
            tv.paint_clr = [UIColor whiteColor]; 
            break; 
        case 1: 
            tv.paint_clr = [UIColor redColor]; 
            break; 
        case 2: 
            tv.paint_clr = [UIColor blueColor]; 
            break; 
        case 3: 
            tv.paint_clr = [UIColor greenColor]; 
            break; 
        case 4: 
            tv.paint_clr = [UIColor yellowColor]; 
            break; 
        default: 
             
            break; 
    } 

 
@end 

效果圖

20151030100650479.png (320×480)

相關(guān)文章

  • Objective-C中的語法糖示例詳解

    Objective-C中的語法糖示例詳解

    開發(fā)過程中我特別喜歡用語法糖,原因很簡單,懶得看到一堆長長的代碼,但語法糖簡單卻不那么簡單,下面這篇文章主要給大家介紹了關(guān)于Objective-C中語法糖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-01-01
  • iOS用兩行代碼完美解決數(shù)據(jù)持久化

    iOS用兩行代碼完美解決數(shù)據(jù)持久化

    所謂的持久化,就是將數(shù)據(jù)保存到硬盤中,使得在應(yīng)用程序或機(jī)器重啟后可以繼續(xù)訪問之前保存的數(shù)據(jù)。在iOS開發(fā)中,有很多數(shù)據(jù)持久化的方案,接下來我將嘗試著介紹一種巧妙的方法,用兩行代碼解決這個問題,一起來學(xué)習(xí)下。
    2016-08-08
  • iOS開發(fā)實現(xiàn)下載器的基本功能(1)

    iOS開發(fā)實現(xiàn)下載器的基本功能(1)

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實現(xiàn)下載器基本功能的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07
  • swift3.0實現(xiàn)圖片放大縮小動畫效果

    swift3.0實現(xiàn)圖片放大縮小動畫效果

    這篇文章主要為大家詳細(xì)介紹了swift3.0實現(xiàn)圖片放大縮小動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Flutter?GetPageRoute實現(xiàn)嵌套導(dǎo)航學(xué)習(xí)

    Flutter?GetPageRoute實現(xiàn)嵌套導(dǎo)航學(xué)習(xí)

    這篇文章主要為大家介紹了Flutter?GetPageRoute實現(xiàn)嵌套導(dǎo)航的示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • iOS實現(xiàn)背景滑動效果

    iOS實現(xiàn)背景滑動效果

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)背景滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 兩種iOS調(diào)用系統(tǒng)發(fā)短信的方法

    兩種iOS調(diào)用系統(tǒng)發(fā)短信的方法

    iOS調(diào)用系統(tǒng)的發(fā)短信功能可以分為兩種:1,程序外調(diào)用系統(tǒng)發(fā)短信。2,程序內(nèi)調(diào)用系統(tǒng)發(fā)短信。第二種的好處是用戶發(fā)短信之后還可以回到app。這對app來說非常重要。
    2016-07-07
  • IOS 添加自定義字體方法詳解

    IOS 添加自定義字體方法詳解

    這篇文章主要介紹了IOS 添加自定義字體方法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法

    iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法

    這篇文章給大家分享了iOS如何設(shè)置UIButton的文字顯示位置和字體的大小、顏色,文中給出了示例代碼,相信對大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們下面來一起看看吧。
    2016-09-09
  • Objective-C中的重載和重寫詳解

    Objective-C中的重載和重寫詳解

    這篇文章主要介紹了Objective-C中的重載和重寫詳解的相關(guān)資料,開發(fā)IOS APP的好多朋友很容易搞錯重載和重寫,這里就詳細(xì)介紹下,需要的朋友可以參考下
    2016-11-11

最新評論