iPad(非常)简单的绘图

2024-03-16

我正在尝试在我的应用程序中实现一个非常简单的绘图视图。这只是我的应用程序的一小部分,但它正在变成一个真正的麻烦。这是我到目前为止所拥有的,但它现在显示的只是莫尔斯电码,如点和线。

  - (void)viewDidLoad {
        [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];

    NSString *savePath  = [NSString stringWithFormat:@"%@/notePadImage.jpg",docsPath];

    NSData *data = [NSData dataWithContentsOfFile:savePath];

    UIImage *image = [UIImage imageWithData:data];

    if (image == nil) {
        NSString *pathToBlank = [[NSBundle mainBundle]pathForResource:@"blankNotePadPage" ofType:@"png"];
        NSData *data = [NSData dataWithContentsOfFile:pathToBlank];
        image = [UIImage imageWithData:data];
    }
    arrayOfTouches = [[NSMutableArray alloc] initWithCapacity:10];
    self.drawImage.image = image;

    mouseMoved = 0;
    [self.view bringSubviewToFront:closeButton];
    [self.view bringSubviewToFront:clearButton];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(drawIt) userInfo:nil repeats:YES];

    }

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


    UITouch *touch = [touches anyObject];

    [arrayOfTouches addObject:touch];




    }


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

    UITouch *touch = [touches anyObject];
    [arrayOfTouches addObject:touch];


    }

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

    }

    -(void) drawIt {

    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:arrayOfTouches];
    [arrayOfTouches removeAllObjects];
    if ([tempArray count]>1) {

        [arrayOfTouches removeAllObjects];
        CGPoint point1 = [[tempArray objectAtIndex:0] previousLocationInView:self.view];;
        CGPoint point2;
        CGPoint point3;

        for (int i = 0; i < [tempArray count]-1;i = i+1) {




            UIGraphicsBeginImageContext(self.view.frame.size);
            [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1.x, point1.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point2.x, point2.y);

            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            [self.view bringSubviewToFront:closeButton];
            [self.view bringSubviewToFront:clearButton];
            point1 = point2;

        }

        }   
    }

我的一个应用程序还需要一些简单的绘图。这是它的一个稍微修改的版本。它的工作原理基本上就像 hotpaw2 所描述的那样。我创建了一个“画布”视图来处理所有绘图,然后将其添加到需要的地方。

对于我的目的来说,速度很好。

CanvasView.h:

@interface CanvasView : UIView {
    NSMutableArray *points;
}
@property (nonatomic, retain) NSMutableArray *points;
@end

CanvasView.m:

#import "CanvasView.h"

@implementation CanvasView

@synthesize points;

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blueColor];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    if (self.points.count == 0)
        return;

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); //white
    CGContextSetLineWidth(context, 1.0);

    CGPoint firstPoint = [[self.points objectAtIndex:0] CGPointValue];
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);

    int i = 1;
    while (i < self.points.count)
    {
        CGPoint nextPoint = [[self.points objectAtIndex:i] CGPointValue];

        if (nextPoint.x < 0 && nextPoint.y < 0)
        {
            CGContextDrawPath(context, kCGPathStroke);

            if (i < (self.points.count-1))
            {
                CGContextBeginPath(context);
                CGPoint nextPoint2 = [[self.points objectAtIndex:i+1] CGPointValue];                
                CGContextMoveToPoint(context, nextPoint2.x, nextPoint2.y);
                i = i + 2;
            }
            else
                i++;
        }
        else
        {
            CGContextAddLineToPoint(context, nextPoint.x, nextPoint.y);
            i++;
        }
    }

    CGContextDrawPath(context, kCGPathStroke);
}

-(void)dealloc
{
    [points release];
    [super dealloc];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    CGPoint location = [touch locationInView:self];

    if (self.points == nil)
    {
        NSMutableArray *newPoints = [[NSMutableArray alloc] init];
        self.points = newPoints;
        [newPoints release];
    }

    [self.points addObject:[NSValue valueWithCGPoint:(location)]];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    CGPoint location = [touch locationInView:self];
    [self.points addObject:[NSValue valueWithCGPoint:(location)]];

    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    CGPoint location = [touch locationInView:self];
    [self.points addObject:[NSValue valueWithCGPoint:(location)]];

    CGPoint endPoint = CGPointMake(-99, -99); //"end of path" indicator
    [self.points addObject:[NSValue valueWithCGPoint:(endPoint)]];

    [self setNeedsDisplay];
}

@end

在需要的地方添加canvasView:

CanvasView *cv = [[CanvasView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
[self.view addSubview:cv];
[cv release];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iPad(非常)简单的绘图 的相关文章

  • iPhone 表格部分页眉和页脚中的文本

    我有一个关于在表视图部分的页眉和页脚中打印文本的问题 我想在每行的页脚中显示一些文本 但这样做时遇到问题 当我开始下一部分时 它从第一个部分的下面开始绘制 要了解我到底需要什么 请转到 设置 gt 常规 gt 网络 在此屏幕中 我们有多个部
  • 为什么分割视图控制器必须始终是您创建的任何界面的根?

    在苹果的开发者指南中 他们指出 分割视图控制器必须始终是您创建的任何界面的根 see here http developer apple com library ios featuredarticles ViewControllerPGfo
  • 从 Xcode 6 安装失败:“存在内部 API 错误”

    我尝试在 ipad ios 7 1 2 上运行一个在我的手机 ios 8 4 1 上运行良好的应用程序 Xcode 提示 存在内部 API 错误 仅此而已 我不确定如何解释日志 怎么了 我该如何解决 ipad日志 Aug 29 17 39
  • 替换核心数据模型,无需迁移

    我已经相当广泛地改变了我的核心数据模型 关于如何将旧数据迁移到新模型中存在很多问题 但是我不需要迁移任何内容 我只想替换当前的 Core Data 实例 如何才能做到这一点 我假设您正在使用持久存储协调器NSSQLiteStoreType
  • 文件中缺少所需的架构 i386

    添加 MapKit 和 CoreLocation 框架后 我在构建应用程序时遇到问题 它们都是 4 3 框架 并且该应用程序过去可以与 UIKit CoreGraphics 和 Foundation 一起正常工作 只是这两个框架给我带来了问
  • 如何显示AVPictureInPictureController?

    我正在尝试使用播放视频AV画中画控制器最近在IOS9 使用此代码 AVPlayer AVPlayer AVPlayer playerWithURL url AVPlayerLayer layer AVPlayerLayer playerLa
  • 可以使用两个独立的 SQLite 数据库吗?

    我有一个 sqlite 数据库 其中存储用户定义的信息和用户只读的信息 我觉得将来可能需要修改只读信息 并且我不想进行整个数据迁移 有没有一种方法可以使用单独的 sqlite 数据库来存储只读信息 该数据库可以轻松替换 如果是这样 您能否就
  • RestKit链接器错误

    我一直遵循 RestKit 安装说明 但现在在尝试构建应用程序时出现错误 这是针对 ios iPad 的 我收到 命令 Developer Platforms iPhoneSimulator platform Developer usr b
  • AVAudioRecorder 标准化音量

    我有一个可以录制音频的应用程序 我想知道如何才能增加增益 有没有办法标准化音频或以某种方式放大它 谢谢 豪伊 看来我找到了解决方案 根据文档 AVAudioPlayer 音量可以在 0 0 到 1 0 之间 显然它可以设置为大于1 0的值
  • iPad 上的 ios11 iphone 应用程序图标丢失

    我有一个仅限 iPhone 的应用程序 但它也可以在 iPad 上以兼容模式运行 毫不奇怪 但是当我更新了ios11的应用程序后 iPad上的应用程序图标丢失了 这是我尝试过的 在iPhone模拟器上启动 出现图标 在 iPad 模拟器上启
  • iOS模拟器找不到SDK,可能需要重新安装SDK

    在这里 我遇到了另一个问题 今天正在开发一个 iOS 应用程序 当我运行 iPhone 5 0 模拟器的应用程序时 仅在该模拟器上遇到了一些奇怪的问题 当我尝试一次又一次地运行该模拟器时 我的 MacBook 屏幕上出现了两个弹出警报窗口
  • 如何命名一段代码并在不同的方法中调用它?

    我使用 Grand Central Dispatch 方法在队列中执行我的应用程序 我在该队列的计算中决定按钮的框架 我希望我的应用程序重新绘制其屏幕并计算旋转后的新帧 这是我所做的一些伪代码解释 CGFloat a 123 b 24 di
  • 如何使用编译时间作为自动版本控制信息?

    当程序被编译以获得一种自动版本信息时 是否有任何常量或 pragma来获取时间和日期 DATE and TIME 是预定义的宏 将分别扩展到当前日期和时间
  • 从 Xcode 更改按钮文本?

    我有一个 IBAction 连接到 Interface Builder 中的一个按钮 是否可以在运行时从我的代码中更改按钮 在 IB 中 上的文本 如果您的代码中有一个连接到某个操作的按钮 则无需实例变量即可更改标题 例如 如果按钮设置为以
  • 使用 json-framework 解析 iphone 应用程序中的 JSON

    我是 iPhone 开发新手 我在我的应用程序中使用货币转换器 我不知道如何用这个 url 解析 jsonhttp www google com calculator http www google com calculator 我想发送我
  • UITextInputMode currentInputMode 已弃用。建议更换?

    在我们的应用程序中 我们想检测当前的键盘语言 例如 如果用户在 设置 gt 常规 gt 键盘 gt 键盘 下设置了多种语言键盘 我们想知道他们正在输入什么语言 并在发生变化时从 NSNotificationCenter 收到通知 void
  • 使用UDID创建唯一的用户身份

    我正在开发一个 iPhone 应用程序 它与服务器通信以存储和交换数据 因为我想让它尽可能简单 所以我想避免用户帐户的注册 或者也可能使用密码 是否有可能 并且允许 获取 iPhone 设备的 UDID 并制作例如 它的 MD5 哈希值 我
  • 如何在 iPad 上使用 HTML5/Javascript 合成音频

    有没有人有工作示例代码 可以在 iPad 上的 Mobile Safari 上使用 HTML5 Javascript 合成 并播放 音频 我在网上找到了一些基于 javascript 的声音合成示例 但它们似乎都只能在 Firefox 中使
  • 如何在 UI 视图上显示两个表

    我想在 UI 视图上使用和显示两个表 请让我知道如何执行此操作 任何相同的代码也将受到赞赏 谢谢 桑迪普 将 2 个 UITableView 添加到 IB 中的视图中 并将它们连接到文件所有者中的 2 个不同的出口 或者简单地分配不同的标签
  • 使用 iOS swift AVPlayer 在横向模式下不全屏

    我将视频视图设置为全屏 然而 在模拟器中玩时 它并没有全屏运行 此问题仅适用于 iPad 不适用于 iPhone 这是我的代码 override func viewDidAppear animated Bool super viewDidA

随机推荐