iOS - 更改 View 的一个像素

标签 ios objective-c colors pixel

在我的应用程序中,我需要将一个像素( View )的颜色更改为黑色,我需要在自定义 gester 识别器的 (void)touchesMoved:withEvent: 中执行此操作,它将应用于 View 。 (我正在制作一支像笔一样的东西)。

我的问题是最简单的在手势识别器后面画一条线是什么,这条线在手势识别器移动后会保留/

如果您需要更多信息,请告诉我。

最佳答案

在 View 中绘图是在drawRect中完成的:http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCDGJHF

您需要在touchesMoved中设置一个实例变量或属性到您需要绘制的点,然后调用[self setNeedsDisplay]并且将调用drawRect。在drawRect中,您将绘制一个单像素矩形。

像这样,修改以满足您的需要:

- (void)drawRect:(CGRect)rect {    
    [super drawRect:rect];  
    CGRect rectangle = CGRectMake(self.cachedPoint.x, self.cachedPoint.y, 1, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextFillRect(context, rectangle);
}

关于iOS - 更改 View 的一个像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17198688/

相关文章:

Android 录制的 aac(.mp3) 文件无法在 iOS 中播放

objective-c - 容器中 View Controller View 的自动布局

ios - 如何在 Xcode 6 的 iOS 中使用 Storyboard执行 self.view.frame.size.width?

css - 绘制边框但不让它继承对象不透明度

ios - UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName 不起作用

ios - View 层次结构中的 Mvvm 交叉绑定(bind)

ios - 移动 Safari 中的网页是否可以检查是否选择了“设置”>“Safari”>“接受 Cookie 'From visited' 或 'Always'”?

ios - 如何在 GitHub Actions 中克隆 CocoaPods 中的私有(private)规范存储库?

iphone - 为动态属性赋值会导致崩溃

python - Matplotlib 中使用 RGB 值的颜色