【问题标题】:How to make create image when screen is touched then be able to move it如何在触摸屏幕时创建图像然后能够移动它
【发布时间】:2014-08-07 06:11:06
【问题描述】:

我试图在触摸屏幕时在屏幕上创建一个点,然后能够移动该点,然后结束触摸,这将显示一个警报视图。对于第一次触摸和移动,到目前为止我有这个代码......

{
    UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
    dot.image=[UIImage imageNamed:@"hockey-puck-stress-toy-superextralarge-175648.png"];
    [self.view addSubview:dot];
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    dot.center = location;
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesBegan:touches withEvent:event];
}

问题是这会在移动时创建一个新对象,而不是在我第一次触摸它时。我明白为什么会这样,但我不知道有什么方法可以解决它。有人可以帮忙吗?谢谢!

【问题讨论】:

    标签: ios touchesbegan touchesmoved


    【解决方案1】:

    在您的 .h 文件中创建一个属性:

    @property (nonatomic) UIImageView *dot;
    

    如果视图不存在则创建视图,然后移动属性:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if (!self.dot) {
            UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
            dot.image=[UIImage imageNamed:@"hockey-puck-stress-toy-superextralarge-175648.png"];
            [self.view addSubview:dot];
        }
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        self.dot.center = location;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多