UI基础 实现最简易画板

来源:互联网 发布:wings战队夺冠 知乎 编辑:程序博客网 时间:2024/06/10 17:39

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

AppDelegate.h


#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (retain,nonatomic) UIWindow *window;


@end



AppDelegate.m


#import"AppDelegate.h"

#import "PaintBoardViewController.h"


@interfaceAppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc {

    [_windowrelease];

    [superdealloc];

}


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

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.windowmakeKeyAndVisible];

    

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

    returnYES;

}


- (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 <UIKit/UIKit.h>


@interface PaintBoardViewController :UIViewController


@end



PaintBoardViewController.m

#import "PaintBoardViewController.h"

#import "PaintView.h"


@interfacePaintBoardViewController ()


@end


@implementation PaintBoardViewController

//重写loadView

- (void)loadView {

    PaintView *paintView = [[PaintViewalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    paintView.backgroundColor = [UIColorwhiteColor];

    paintView.lineWidth =5;

    paintView.lineColor = [UIColorblackColor];

   self.view = paintView;

    [paintViewrelease];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    //创建菜单按钮

    UIButton *menuButton = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

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

    [self.viewaddSubview:menuButton];

    

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

    textField.tag =123;

    [self.viewaddSubview:textField];

    [textFieldrelease];

    

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

    inputView.backgroundColor = [UIColorwhiteColor];

    textField.inputView = inputView;

    [inputViewrelease];

    

    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 = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

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

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

        [button setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        

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

        button.layer.cornerRadius = width /2.0;

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

        

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

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

        [inputViewaddSubview: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:  //撤销按钮

            [paintViewundoLastDrawing];

           break;

       case 103:  //清空按钮

            [paintViewallClean];

           break;

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

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

           //设置画笔颜色

            paintView.lineColor = sender.backgroundColor;

           break;

       default:

           break;

    }

}


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

    //获取textField

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

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

   if (textField.isFirstResponder) {

        [textFieldresignFirstResponder];

    }else {

        [textFieldbecomeFirstResponder];  //成为第一响应者

    }

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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 <UIKit/UIKit.h>


@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];

    [superdealloc];

}


- (NSMutableArray *)allLines {

   if (!_allLines) {

       self.allLines = [NSMutableArrayarray];

    }

    return_allLines;

}


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

   UITouch *aTouch = touches.anyObject;

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


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

    UIBezierPath *bezierPath = [UIBezierPathbezierPath];

    [bezierPathmoveToPoint:startPoint];


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

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

    line.path = bezierPath;

    line.colcor = self.lineColor;

    line.width =self.lineWidth;


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

    [self.allLinesaddObject:line];

}


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

   UITouch *aTouch = touches.anyObject;

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


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

   LineModel *aline = self.allLines.lastObject;

    [aline.pathaddLineToPoint:currentPoint];


    [selfsetNeedsDisplay]; //调用此方法是通知视图,绘制界面,一旦调用此方法,系统就会自动调用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 *aLinein self.allLines) {

       //设置填充颜色

        [aLine.colcorsetStroke];

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

        [aLine.pathsetLineWidth:aLine.width];

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

    }

}


- (void)undoLastDrawing {

    [self.allLinesremoveLastObject];

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

}


- (void)allClean {

    [self.allLinesremoveAllObjects];

    [selfsetNeedsDisplay];

}

@end



LineModel.h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@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 {

    [_pathrelease];

    [_colcorrelease];

    [superdealloc];

}

@end






0 0
原创粉丝点击