是否可以更改特定 x 和 y 坐标的颜色?
Is it possible to change colour for particular x and y coordinate?
例如看看这个:
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
path = [UIBezierPath bezierPathWithRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1)];
NSLog(@"Touch moving point =x : %f Touch moving point =y : %f", touchPoint.x, touchPoint.y);
}
我尝试了什么:
C
GRect topRect = CGRectMake(touchPoint.x,touchPoint.y, 10,10);
// Fill the rectangle with grey
[[UIColor redColor] setFill];
UIRectFill( topRect );
[self.view drawRect:topRect];
但它给我错误:
<Error>: CGContextSetCompositeOperation: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
当用户移动触摸时,我得到坐标并改变颜色。
当用户移动他们的手时,x 和 y 坐标正在生成,对于那个特定的地方,我想改变颜色。
如果可能请给点提示。
任何绘图操作都必须在view drawing cycle内完成。你不能在任何时候绘制东西,你只能在绘制视图时这样做。
这意味着覆盖你的 UIView 子类中的 drawRect:
方法,如果你没有,那么你需要添加它:
- (void)drawRect:(CGRect)rect {
GRect topRect = CGRectMake(self.touchedPoint.x,touchedPoint.y, 10,10);
// Fill the rectangle with grey
[[UIColor redColor] setFill];
UIRectFill( topRect );
[self.view drawRect:topRect];
}
下一步是将信息从您的控制器(接收 touchesMoved:
消息的控制器)传递到视图。这可以通过视图上的 属性 来完成:
@property (nonatomic) CGPoint touchedPoint;
最后,在传递点详细信息后,您需要通过调用 setNeedsDisplay:
告诉视图它需要重绘
((MyViewSubclass*)self.view).touchedPoint = touchPoint;
[self.view setNeedsDisplay];
由于@Cristik 的回答非常正确,让我补充一下如何注入触摸事件。作为 CGPoint
属性 进行触摸的问题是您不知道什么时候没有触摸。其次,您将无法注入多点触摸。
所以最好在您的视图中使用数组。 NSValue
可以帮助您将点存储为对象:
// assuming you have a property:
// @property (nonatomic, strong) NSMutableArray *touchesYetToProcess;
- (void)injectTouchAtLocation:(CGPoint)touchPoint {
NSMutableArray *items = self.touchesYetToProcess; // a property holding the touches
[items addObject:[NSValue valueWithCGPoint:touchPoint]];
}
- (void)drawRect:(CGRect)rect {
NSMutableArray *items = self.touchesYetToProcess;
NSArray *newTouches = [items copy]; //= copy the touches
[items removeAllObjects]; // clear the buffer
for(NSValue *touchValue in newTouches) {
CGPoint point = touchValue.CGPointValue;
// do whatever you want with the point
}
}
例如看看这个:
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
path = [UIBezierPath bezierPathWithRect:CGRectMake(touchPoint.x, touchPoint.y, 1, 1)];
NSLog(@"Touch moving point =x : %f Touch moving point =y : %f", touchPoint.x, touchPoint.y);
}
我尝试了什么: C
GRect topRect = CGRectMake(touchPoint.x,touchPoint.y, 10,10);
// Fill the rectangle with grey
[[UIColor redColor] setFill];
UIRectFill( topRect );
[self.view drawRect:topRect];
但它给我错误:
<Error>: CGContextSetCompositeOperation: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
当用户移动触摸时,我得到坐标并改变颜色。 当用户移动他们的手时,x 和 y 坐标正在生成,对于那个特定的地方,我想改变颜色。
如果可能请给点提示。
任何绘图操作都必须在view drawing cycle内完成。你不能在任何时候绘制东西,你只能在绘制视图时这样做。
这意味着覆盖你的 UIView 子类中的 drawRect:
方法,如果你没有,那么你需要添加它:
- (void)drawRect:(CGRect)rect {
GRect topRect = CGRectMake(self.touchedPoint.x,touchedPoint.y, 10,10);
// Fill the rectangle with grey
[[UIColor redColor] setFill];
UIRectFill( topRect );
[self.view drawRect:topRect];
}
下一步是将信息从您的控制器(接收 touchesMoved:
消息的控制器)传递到视图。这可以通过视图上的 属性 来完成:
@property (nonatomic) CGPoint touchedPoint;
最后,在传递点详细信息后,您需要通过调用 setNeedsDisplay:
告诉视图它需要重绘((MyViewSubclass*)self.view).touchedPoint = touchPoint;
[self.view setNeedsDisplay];
由于@Cristik 的回答非常正确,让我补充一下如何注入触摸事件。作为 CGPoint
属性 进行触摸的问题是您不知道什么时候没有触摸。其次,您将无法注入多点触摸。
所以最好在您的视图中使用数组。 NSValue
可以帮助您将点存储为对象:
// assuming you have a property:
// @property (nonatomic, strong) NSMutableArray *touchesYetToProcess;
- (void)injectTouchAtLocation:(CGPoint)touchPoint {
NSMutableArray *items = self.touchesYetToProcess; // a property holding the touches
[items addObject:[NSValue valueWithCGPoint:touchPoint]];
}
- (void)drawRect:(CGRect)rect {
NSMutableArray *items = self.touchesYetToProcess;
NSArray *newTouches = [items copy]; //= copy the touches
[items removeAllObjects]; // clear the buffer
for(NSValue *touchValue in newTouches) {
CGPoint point = touchValue.CGPointValue;
// do whatever you want with the point
}
}