[英]How can I make this code smoother. I am trying to draw triangle and with one touch (touches moved) i want it resize
如何使此代碼更流暢。 我正在嘗試繪制三角形,並且一鍵(觸摸已移動)我想要調整其大小。 這是我的代碼:
-(id)initWithPoint:(CGPoint )point withFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
givenPoint = point;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1);
CGPoint points[6] = { CGPointMake(10, 10), CGPointMake(50, 10),
CGPointMake(50, 10), givenPoint,
givenPoint, CGPointMake(10, 10) };
CGContextSetRGBFillColor(ctx, 255, 255, 255, 1);
CGContextStrokeLineSegments(ctx, points, 6);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
UIView *view = touch.view;
[view removeFromSuperview];
TailView *tailView = [[TailView alloc] initWithPoint:CGPointMake(location.x, location.y) withFrame:CGRectMake(100, 100, location.x + 40, location.y + 40)];
tailView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tailView];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[tailView addGestureRecognizer:panRecognizer];
}
簡而言之,drawRect很慢。 如果希望平滑,則應使用轉換。 不要將視圖從超級視圖中刪除並再次添加它,從而強制重繪,而應應用縮放和旋轉變換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.