在Objective-C中绘制

杰森·福斯特(Jason Foster)

我知道您可以使用CoreGraphics在视图中绘制。但这仅限于drawRect函数。

我想知道您是否可以使用一个具有两个按钮(上,下,左,右)的应用程序。而且,当用户选择按钮时,它会从原始点沿刚刚选择了20像素的按钮方向绘制一条线。

例如 :

假设用户点击了右键:

              Start
                +----------+

然后,他们按下向下按钮:

              Start
                +----------+
                           |
                           |
                           |
                           |
                           +

然后他们按左键

                +----------+
                           |
                           |
                           |
                           |
                +----------+

等等,

可以使用石英完成此操作,还是需要学习OpenGL之类的知识?

注射剂

有了UIBezierPath它很容易:

这是一个快速,基本的示例,如何绘制它

  1. 假设UIView子类MoveView具有公共方法-moveToDirection:
  2. 查看阵列中的商店方向
  3. 每次添加新方向时,我们都会调用-setNeedsDisplay绘制新线

注意:这只是基本示例,请确保您需要对其进行修改并实施一些限制

MoveView.h

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, MovieViewDirection) {
    MoveViewDirectionRight,
    MoveViewDirectionLeft,
    MoveViewDirectionUp,
    MoveViewDirectionDown
};

@interface MoveView : UIView

- (void)moveInDirection:(MovieViewDirection)direction;

@end

MoveView.m

#import "MoveView.h"

static const CGFloat kMoveViewStepDistance = 20.0f;

@interface MoveView ()

@property (nonatomic, strong) NSArray *movingDirections;

@end

@implementation MoveView

#pragma mark - Services

- (void)drawRect:(CGRect)rect {

    UIBezierPath* bezierPath = UIBezierPath.bezierPath;        

    CGPoint currentPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));        
//    Start point by default it is a center
    [bezierPath moveToPoint: currentPoint];

    for (NSNumber *direction in self.movingDirections) {
        CGPoint moveToPoint;
        switch (direction.integerValue) {
            case MoveViewDirectionLeft:
                moveToPoint = CGPointMake(currentPoint.x - kMoveViewStepDistance, currentPoint.y);
                break;
            case MoveViewDirectionRight:
                moveToPoint = CGPointMake(currentPoint.x + kMoveViewStepDistance, currentPoint.y);
                break;
            case MoveViewDirectionUp:
                moveToPoint = CGPointMake(currentPoint.x, currentPoint.y - kMoveViewStepDistance);
                break;
            case MoveViewDirectionDown:
                moveToPoint = CGPointMake(currentPoint.x, currentPoint.y + kMoveViewStepDistance);
                break;                
            default:
                break;
        }
        currentPoint = moveToPoint;
        [bezierPath addLineToPoint: moveToPoint];
    }

    [UIColor.redColor setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];

}

#pragma mark - Public
- (void)moveInDirection:(MovieViewDirection)direction {
    [self addMoveStepInDirection:direction];
    [self setNeedsDisplay];
}

#pragma mark - Private

- (void)addMoveStepInDirection:(MovieViewDirection)direction {
    NSMutableArray *steps = [NSMutableArray arrayWithArray:self.movingDirections];
    [steps addObject:[NSNumber numberWithInteger:direction]];
    self.movingDirections = steps;
}

@end

这就是我得到的:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章