iOS-画板程序(手势操作无)

//
//  HMPaintView.h
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HMPaintView : UIView

@property(nonatomic,assign)CGFloat width;

@property(nonatomic,strong)UIColor *color;

@property(nonatomic,strong)UIImage *image;

- (void)clearScreen;

- (void)undo;

@end

//
//  HMPaintView.m
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMPaintView.h"
#import "HMPaintPath.h"
@interface HMPaintView()

@property(nonatomic,strong)UIBezierPath *path;

@property(nonatomic,strong)NSMutableArray *paths;

@end

@implementation HMPaintView

- (NSMutableArray *)paths
{
    if (_paths == nil) {
        _paths = [NSMutableArray array];
    }

    return _paths;
}
- (CGPoint)pointWithTouches:(NSSet *)touches
{
    UITouch *touch = [touches anyObject];
    return [touch locationInView:self];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pos = [self pointWithTouches:touches];

    HMPaintPath *path = [HMPaintPath paintPathWithLineWidth:_width color:_color startPoint:pos];

    _path =path;
    [self.paths addObject:path];

}
-(void)awakeFromNib
{
    _width = 2;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pos = [self pointWithTouches:touches];
    [_path addLineToPoint:pos];
    [self setNeedsDisplay];
}

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

    if (!self.paths.count) return;

    for (HMPaintPath *path in self.paths) {
        if ([path isKindOfClass:[UIImage class]]) {
             UIImage *image = (UIImage *)path;
        [image drawAtPoint:CGPointZero];

        }else
        {
             [path.color set];
        [path stroke];
        }

    }
}

- (void)clearScreen
{
    [self.paths removeAllObjects];
    [self setNeedsDisplay];
}

-(void)undo
{
    [self.paths removeLastObject];
    [self setNeedsDisplay];

}

- (void)setIamge:(UIImage *)image
{
    _image = image;
    [self.paths addObject:image];

    [self setNeedsDisplay];
}

@end
//
//  HMPaintPath.h
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HMPaintPath : UIBezierPath

@property(nonatomic,strong)UIColor *color;

+ (instancetype)paintPathWithLineWidth:(CGFloat)width color:(UIColor *)color startPoint:(CGPoint)startP;
@end

//
//  HMPaintPath.m
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMPaintPath.h"

@implementation HMPaintPath

+ (instancetype)paintPathWithLineWidth:(CGFloat)width color:(UIColor *)color startPoint:(CGPoint)startP
{
    HMPaintPath *path = [[self alloc] init];

    path.lineWidth = width;

    path.color = color;
    [path moveToPoint:startP];
    return path;
}
@end
//
//  HMHandleImageView.h
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HMHandleImageView : UIView

@end

------------手势操作------------------

//
//  HMHandleImageView.m
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMHandleImageView.h"

@implementation HMHandleImageView

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

@end
//
//  ViewController.h
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

//
//  ViewController.m
//  画板
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"
#import "HMPaintView.h"
#import "MBProgressHUD+MJ.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet HMPaintView *paintView;

@end

@implementation ViewController
- (IBAction)save:(id)sender {
    UIGraphicsBeginImageContextWithOptions(_paintView.bounds.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [_paintView.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [MBProgressHUD showError:@"保存失败"];
    }else
    {
        [MBProgressHUD showSuccess:@"保存成功"];
    }
}

- (IBAction)selectPicture:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];

    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *image = info[UIImagePickerControllerOriginalImage];
    _paintView.image = image;

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)eraser:(id)sender {
    _paintView.color = [UIColor lightGrayColor];
}

- (IBAction)undo:(id)sender {
    [_paintView undo];

}

- (IBAction)clearScreen:(id)sender {
    [_paintView clearScreen];
}

- (IBAction)colorClick:(UIButton *)sender {
    _paintView.color = sender.backgroundColor;
}

- (IBAction)valueChange:(UISlider *)sender {
    _paintView.width = sender.value;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
时间: 2024-10-24 16:43:00

iOS-画板程序(手势操作无)的相关文章

IOS学习之 手势操作

参考文章 http://blog.jobbole.com/65846/ 1. UIGestureRecognizer介绍 UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别. UITapGestureRecognizer                // 点击     UIPinchGestureRecognizer            // 二指往內或往外拨动,平时经常用到的缩放     UIRotationGestureRecogn

ios的手势操作之UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

【转】 ios的手势操作之UIGestureRecognizer浅析

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

iOS开发——仿Clear纯手势操作的UITableView

前言 在Clear应用中,用户无需任何按钮,纯靠不同的手势就可以完成对ToDoItem的删除.完成.添加.移动.具体来说,功能上有左划删除,右划完成,点击编辑,下拉添加.捏合添加.长按移动.这里将这些功能实现并记录. 左划删除与右划完成 所谓的左右滑动,就是自定义一个cell然后在上面添加滑动手势.在处理方法中计算偏移量,如果滑动距离超过cell宽度一半,就删除它,或者是为文本添加删除线等来完成它:如果没有超过一半,那么就用动画把cell归位. 效果图如下: 关键代码如下: - (void)ha

说说iOS中的手势及触摸

一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类,响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次获得响应事件消息的机会.当发生事件时,事件首先被发送给第一响应者,第一响应者往往是事件发生的视图,也就是用户触摸屏幕的地方.事件将沿着响应者链一直向下传递,直到被接受并做出处理.一般来说,第一响应者是个视图对象或者其子类对

FingerGestures研究院之初探Unity手势操作

最近研究了一下Unity中的一个手势操作的插件FingerGestures.它能很方便监听到Unity中的各种手势事件:上下左右四方向的滑动事件.按下事件.抬起事件.移动事件.连击事件.长按事件等等.它同时支持触摸屏操作与鼠标操作,总起来说使用起来还是比较方便的,今天写下教程记录这个插件的详细使用步骤.首先下载这个插件,大家可以在圣典上找这个插件的下载地址,当然也可以在本文最后下载该插件.  我看了一下这个插件底层的实现步骤,他是通过C#代理的形式来实现手势操作的.如下图红圈内所示,这五个重要的

在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤

原文:在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤 Xamarin 配置手册和离线包下载 http://pan.baidu.com/s/1eQ3qw8a 具体操作: 安装前提条件 1. 安装Visual Studio 2013,安装过程省略,我这里安装的windows10 + vs2013 with update 4. 2. 安装Java SDK,按照Next一步步安装,此处省略,如下图: 3. 安装Android SDK:因为在线安装的访