用Objective-C绘图

时间:2014-09-21 05:57:28

标签: ios objective-c

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

我想知道你是否有一个应用程序,它有几个按钮(向上,向下,向左,向右)。并且当用户选择按钮时,它从按钮的方向上的原始点绘制一条线,该按钮刚刚被20像素选择。

例如:

假设用户点击了右键:

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

然后他们点击向下按钮:

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

然后他们点击左键

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

这可以使用石英完成,还是需要学习像OpenGL这样的东西?

1 个答案:

答案 0 :(得分:2)

使用UIBezierPath非常简单:

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

  1. UIView子类让我们说MoveView,使用公共方法-moveToDirection:
  2. 查看在数组中存储路线
  3. 每次添加新方向时,我们都会调用-setNeedsDisplay来绘制新行
  4. 注意:只是基本示例,确定您需要修改它并实施一些限制

    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
    

    这就是我得到的:

    enter image description here