我知道您可以使用CoreGraphics绘制视图。但它仅限于drawRect函数。
我想知道你是否有一个应用程序,它有几个按钮(向上,向下,向左,向右)。并且当用户选择按钮时,它从按钮的方向上的原始点绘制一条线,该按钮刚刚被20像素选择。
例如:
假设用户点击了右键:
Start
+----------+
然后他们点击向下按钮:
Start
+----------+
|
|
|
|
+
然后他们点击左键
+----------+
|
|
|
|
+----------+
等
这可以使用石英完成,还是需要学习像OpenGL这样的东西?
答案 0 :(得分:2)
使用UIBezierPath
非常简单:
这是一个快速,基本的例子,如何绘制它
MoveView
,使用公共方法-moveToDirection:
-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
这就是我得到的: