ios - 我的形状不会出现

标签 ios xcode uitouch

你好,我想在 iOS 的 UITouch 中围绕一个点画一个圆圈。我也想画在该点相交的十字线,但我还没有走那么远。这应该能够在最多 10 个接触点发生,这就是为什么您会在开头看到 for 循环的原因。任何帮助将非常感激。

我在全局变量中列出了点,因为我在 touchesBegantouchesMovedtouchesEnded 中使用了它。我需要显示形状,直到启动新的触摸点。我对这一切还很陌生,有点不知所措。

谢谢你的时间。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    radius = 10;
    CGContextRef context = UIGraphicsGetCurrentContext();

    for (UITouch *touch in touches)
    {


        point = [touch locationInView:self.view];
        CGContextSetRGBStrokeColor(context, 255.0, 255.0, 0.0, 1.0);
        CGContextAddArc(context, point.x, point.y, radius, 0, M_PI*2, YES);
        CGContextStrokePath(context);

        NSLog(@"X: %.3f Y: %.3f Began", point.x, point.y);
    }

    self.timestampstart = [NSDate date];

}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    self.timestampend = [NSDate date];
    self.duration.text = [[NSString alloc] initWithFormat:@"%.3f s", [self.timestampend timeIntervalSinceDate:self.timestampstart]];

    NSLog(@"X: %.3f Y: %.3f Ended. Duration %.3f", point.x, point.y, [self.timestampend timeIntervalSinceDate:self.timestampstart]);

}

最佳答案

我认为问题在于您不能在此方法中使用 Core Graphics。通常,UIViewdrawRect 方法是您要使用它的地方。

也许使用 UITapGestureRecognizer 添加自定义 UIView 子类(代表您的圈子)会更好?因此,您非常关心它所属的位置:什么当有人点击 View 时应该发生什么,您绘制的对象应该是什么样子。两个问题,很好地分开了。 (关注点分离是或者至少将是你的 friend ,相信我。)

关于ios - 我的形状不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626830/

相关文章:

ios - 过滤 BehaviorRelay 数组 rxswift

ios - 为什么在 iOS 上,glRenderbufferStorage 似乎失败了?

xcode - 在 Xcode 中搜索未找到结果(搜索我的源代码)

iphone - 如何在 XCode 5 中添加 57x57 图标

ios - 如何防止像 iPhone 照片应用程序那样自动旋转?

ios - UIView:继承触摸

ios - 如何裁剪图像以具有弯曲的不规则边缘?

ios - swift : scroll the view up when keyboard shows

ios - xcode 5 本地化失败

iphone - 应用 CGAffineTransform Rotate 后如何移动 UIImageView?