Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在objective c中绘制iPhone_Iphone_Objective C - Fatal编程技术网

在objective c中绘制iPhone

在objective c中绘制iPhone,iphone,objective-c,Iphone,Objective C,我对编程非常陌生,我已经制作了一半(简单的)应用程序,但我想知道如何在屏幕上绘制图片(用户绘制图片),然后在游戏中使用该图像左右移动(并检查它是否与另一个图像碰撞) 我有这个 float pointx; float pointy; - (void)drawRect:(CGRect)rect { CGColorRef blue = [[UIColor blueColor] CGColor]; CGContextRef context = UIGraphicsGetCurrentContext

我对编程非常陌生,我已经制作了一半(简单的)应用程序,但我想知道如何在屏幕上绘制图片(用户绘制图片),然后在游戏中使用该图像左右移动(并检查它是否与另一个图像碰撞)

我有这个

float pointx;
float pointy;


- (void)drawRect:(CGRect)rect {
CGColorRef blue = [[UIColor blueColor] CGColor];

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);

CGContextSetFillColorWithColor(context, blue);
CGContextFillRect(context, CGRectMake(pointx, pointy, 10, 10));

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch=[[event allTouches]anyObject];
CGPoint point = [touch locationInView:touch.view];
pointx = point.x;
pointy = point.y;
[self setNeedsDisplay];
}

但当我按下屏幕上的按钮时,一个蓝色的方块会移到手指上,但不会画任何东西。

使用Cocoa Touch绘制用户生成的图片是一个两步过程。UIView只会在清除所有以前用户绘制的内容后绘制您手上的最新触摸

一种可能的解决方案是将所有用户触摸保存在历史记录数组中,并在添加任何新触摸后(重新)将所有用户触摸绘制到视图中。但这可能非常缓慢,具体取决于所需的绘图量


另一种可能的两步方法是创建自己的位图绘图上下文。首先将最新的内容绘制到该上下文中,如果配置正确,该上下文将保留图形的旧部分,然后将该上下文绘制到UIView中(或将位图转换为显示在视图上的图层中的图像)。

创建一个类,该类是UIView的子类。。。然后在tat中添加这些代码行

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self];
[currentPath moveToPoint:(gestureStartPoint)];
}

}

在头文件中声明以下内容

CGPoint gestureStartPoint,currentPosition;
UIBezierPath *currentPath;
并声明一个属性

@属性(非原子,保留)UIBezierPath*currentPath

在if块内的initWIthFrame方法中,添加这些行

currentPath = [[UIBezierPath alloc]init];
currentPath.lineWidth=3;
创建viewcontroler类,然后在loadVIew方法中添加这些代码行

mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
mainView.backgroundColor=[UIColor whiteColor];
self.view=mainView;
其中sampleView是u创建的UIView子类b4


希望这有帮助…

所以你想在用户手指在屏幕上移动时绘制一张图片?很抱歉我的格式化方式…booleanBoy,你还没有声明mainView…mainView是全局的。。。我已经在头文件中声明了它。。。。希望你能找到我…为什么你不使用c却要声明它?我发誓你只声明了:CGPoint手势startpoint,currentPosition;CGContextRef c;UIBezierPath*当前路径;
currentPath = [[UIBezierPath alloc]init];
currentPath.lineWidth=3;
mainView=[[sampleView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
mainView.backgroundColor=[UIColor whiteColor];
self.view=mainView;