随着UIBezierPath
它很容易:
这是一个快速,简单的例子如何绘制它
- UIView子类比方说
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
这是我得到了什么:
![enter image description here](https://i.stack.imgur.com/xfCK4.png)
您可以使用UIBezier路径。 – WMios 2014-09-21 06:09:03
为什么不在'-drawRect:'中做? – 2014-09-21 06:40:25