您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

用 iOS 打造一個簡單的繪畫板

編輯:iOS 相關教學

本文分享一個在 iOS 上實作基本繪畫板功能的簡單做法,核心在於自訂 UIView 並記錄觸控點的軌跡。

TouchView.h

#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

#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.
- (void)drawRect:(CGRect)rect
{
    if ((!self.points) || (self.points.count < 2)) {
        return;
    }

    context = UIGraphicsGetCurrentContext();
    // 設置畫筆粗細
    CGContextSetLineWidth(context, 5.0f);
    // 設置畫筆顏色
    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;
}

// 建立一個 array,並記錄初始 point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.points = [NSMutableArray array];
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self.points addObject:[NSValue valueWithCGPoint:pt]];
}

// 移動過程中記錄這些 points
// 呼叫 setNeedsDisplay 會觸發 drawRect 方法的呼叫
- (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

#import <UIKit/UIKit.h>

@class TouchView;
@interface ViewController : UIViewController
{
    TouchView *tv;
}
@end

ViewController.m

在這裡,我們用一個 UISegmentedControl 做為調色盤,切換不同顏色:

#import "ViewController.h"
#import "TouchView.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.userInteractionEnabled = YES;

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

最終的執行效果如下:

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved