UI基础 实现最简易画板

在模拟器上实现简易画板的功能,只用到UILabel,UITextField,UIButton的功能.

AppDelegate.h


#import


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic) UIWindow *window;


@end



AppDelegate.m


#import "AppDelegate.h"

#import "PaintBoardViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc {

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    self.window.rootViewController = [[[PaintBoardViewController alloc] init] autorelease];

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end



PaintBoardViewController.h

#import


@interface PaintBoardViewController : UIViewController


@end



PaintBoardViewController.m

#import "PaintBoardViewController.h"

#import "PaintView.h"


@interface PaintBoardViewController ()


@end


@implementation PaintBoardViewController

//重写loadView

- (void)loadView {

    PaintView *paintView = [[PaintView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    paintView.backgroundColor = [UIColor whiteColor];

    paintView.lineWidth = 5;

    paintView.lineColor = [UIColor blackColor];

    self.view = paintView;

    [paintView release];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //创建菜单按钮

    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeSystem];

    menuButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds) - 80, 40, 60, 40);

    [menuButton setTitle:@"菜单" forState:UIControlStateNormal];

    [menuButton addTarget:self action:@selector(handleMenuButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:menuButton];

    

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero];

    textField.tag = 123;

    [self.view addSubview:textField];

    [textField release];

    

    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 60)];

    inputView.backgroundColor = [UIColor whiteColor];

    textField.inputView = inputView;

    [inputView release];

    

    NSArray *titles = @[@"减小",@"增大",@"撤销",@"清空",@"颜色"];

    CGFloat width = (CGRectGetWidth(self.view.bounds) - 20 * 6) / 5;

    CGFloat height = width;

    CGFloat originY = (60 - height) / 2;

    CGFloat originX = 20;

    

    for (int i = 0; i < titles.count; i ++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.tag = 100+i;     //设置5个按钮的值

        button.frame = CGRectMake(originX + (width + originX) * i, originY, width, height);

        [button setTitle:titles[i] forState:UIControlStateNormal];

        button.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.6];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        

        //设置按钮的角半径,为按钮宽度的一半

        button.layer.cornerRadius = width / 2.0;

        button.layer.masksToBounds = YES;   //按指定的半径裁剪视图

        

        //给五个按钮绑定响应方法

        [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [inputView addSubview:button];

    }

}


//5个按钮关联的方法

- (void)handleButtonAction:(UIButton *)sender {

    PaintView *paintView = (PaintView *)self.view;   //获取画板视图

    switch (sender.tag) {

        case 100:   //减小按钮

            if (paintView.lineWidth > 2) {

                paintView.lineWidth -= 1;     // <0画不出来

            }

            break;

        case 101:   //增大按钮

            paintView.lineWidth += 1;

            break;

        case 102:   //撤销按钮

            [paintView undoLastDrawing];

            break;

        case 103:   //清空按钮

            [paintView allClean];

            break;

        case 104:   //颜色按钮:随机产生一个颜色

            sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

            //设置画笔颜色

            paintView.lineColor = sender.backgroundColor;

            break;

        default:

            break;

    }

}


- (void)handleMenuButtonAction:(UIButton *)sender {

    //获取textField

    UITextField *textField = (UITextField *)[self.view viewWithTag:123];

    //判断如果textField当前是第一响应者,则撤销其第一响应者权限,否则让其成为第一响应者

    if (textField.isFirstResponder) {

        [textField resignFirstResponder];

    } else {

        [textField becomeFirstResponder];  //成为第一响应者

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end



PaintView.h

#import


@interface PaintView : UIView


@property (nonatomic, retain) NSMutableArray * allLines; //用于保存画板上得所有线条对象

@property (nonatomic, retain) UIColor *lineColor; //当前的线条的颜色

@property (nonatomic, assign) CGFloat lineWidth; //当前的线条宽度


- (void)undoLastDrawing; //撤销上次的绘制

- (void)allClean; //清空画板

@end



PaintView.m

#import "PaintView.h"

#import "LineModel.h"


@implementation PaintView


- (void)dealloc {

    [_allLines release];

    [_lineColor release];

    [super dealloc];

}


- (NSMutableArray *)allLines {

    if (!_allLines) {

        self.allLines = [NSMutableArray array];

    }

    return _allLines;

}


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

    UITouch *aTouch = touches.anyObject;

    CGPoint startPoint = [aTouch locationInView:self.superview];


    //创建贝塞尔曲线对象,并设置曲线的起始点

    UIBezierPath *bezierPath = [UIBezierPath bezierPath];

    [bezierPath moveToPoint:startPoint];


    //创建线条模型对象,并保存当前线条的三个数据

    LineModel *line = [[[LineModel alloc] init] autorelease];

    line.path = bezierPath;

    line.colcor = self.lineColor;

    line.width = self.lineWidth;


    //添加到数组中等待使用

    [self.allLines addObject:line];

}


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

    UITouch *aTouch = touches.anyObject;

    CGPoint currentPoint = [aTouch locationInView:self.superview];


    //获取对应的线条模型(在触摸移动的过程中,数组的最后一个对象对应当前正在绘制的曲线)

    LineModel *aline = self.allLines.lastObject;

    [aline.path addLineToPoint:currentPoint];


    [self setNeedsDisplay];  //调用此方法是通知视图,绘制界面,一旦调用此方法,系统就会自动调用drawRect:方法来绘制界面.

}


// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    for (LineModel *aLine in self.allLines) {

        //设置填充颜色

        [aLine.colcor setStroke];

        //设置绘制时的线条宽度

        [aLine.path setLineWidth:aLine.width];

        [aLine.path stroke];   //开始绘制曲线

    }

}


- (void)undoLastDrawing {

    [self.allLines removeLastObject];

    [self setNeedsDisplay];   //移除数组中的最后一条曲线对象,并重绘界面

}


- (void)allClean {

    [self.allLines removeAllObjects];

    [self setNeedsDisplay];

}

@end



LineModel.h

#import

#import


@interface LineModel : NSObject


@property (nonatomic, retain) UIBezierPath *path; //使用贝塞尔曲线记录触摸轨迹

@property (nonatomic, retain) UIColor *colcor; //线条颜色

@property (nonatomic, assign) CGFloat width; //线条宽度


@end



LineModel.m

#import "LineModel.h"


@implementation LineModel


- (void)dealloc {

    [_path release];

    [_colcor release];

    [super dealloc];

}

@end






你可能感兴趣的:(代码)