图像应遵循iphone中之前的点触摸?

时间:2011-12-12 09:19:17

标签: iphone objective-c ios xcode

enter image description here

触摸开始时 - 显示图像,触摸时图像跟随触摸事件,触摸结束时图像消失

 - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
        [super touchesBegan:touches withEvent:event];
        newgicg.hidden = NO;
        NSArray *allTouches = [touches allObjects]; 
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInView:touch.view];
        int count = [allTouches count];
        if (count == 1) {

            newgicg.center = touchLocation;           

            }

    } 

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

        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInView:touch.view];

         newgicg.center = touchLocation;     

       [self.view addSubview:newgicg];
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];

         newgicg.hidden = YES;


    }

我想要一些类似的东西,当光标在屏幕上移动时跟随星星的集合。我希望星星闪烁并消失在光标在屏幕上的任何地方

1 个答案:

答案 0 :(得分:2)

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

} 


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

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"simple-apple.png"]];
    imageView.center = touchLocation;    
    [self.view addSubview:imageView];
    [imageView release];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    for (UIView *view in [self.view subviews]) {
        [view removeFromSuperview];
    }

}

尝试使用此代码,只需替换图像的名称。

编辑: 我忘了提到的一件事是,你可以坚持反对。以这种方式添加的子视图并删除它们,否则它也可能会删除之前视图中的其他视图。