iphone - 图像应该遵循 iphone 中以前的触摸点?

标签 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;


    }

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

最佳答案

- (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];
    }

}

请尝试使用此代码,只需替换图片的名称即可。

编辑: 我忘记提到的一件事是,您可以保留一个柜台。以这种方式添加并删除它们的 subview ,否则它也可能会删除之前存在于 View 中的其他 View 。

关于iphone - 图像应该遵循 iphone 中以前的触摸点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8472116/

相关文章:

ios - NSMutableArray 全局

ios - 未安装应用程序时,iOS 深层链接会发生什么情况?

iphone - 适用于 iPhoneSDK 的 Bing map

iphone - NSFetchedResultsController sectionIndexTitles 错误不是英文字母

ios - Xcode Cocos2D - 警告所有应用程序应包含 armv7 架构

ios - 检测热点在 iPhone X 中崩溃

ios - Cocos2d : How to check Sprite Frame Name is available or not?

ios - Safari 无法在 iOS 9 中打开 iOS 应用程序

iphone - 向 iOS 应用添加 "Open In..."选项

javascript - 在 iOS 上配置 javascript