《30天精通iPhone手机编程》-Day28-触屏移动

来源:互联网 发布:电脑网络技术培训 编辑:程序博客网 时间:2024/06/02 09:08

        在原来的游戏中经常的动作就是点到什么地方,游戏中的任务就跑到什么地方,这在网游中尤其常见,下面实现的就是在屏幕上面点击某个位置,设置的图片就到手指所点的那个坐标位置去。

//播放音频文件  -(void)playSound:(NSString*)soundAct{//音频文件的路径名称  NSString *path = [NSString stringWithFormat:@"%@%@",  [[NSBundle mainBundle] resourcePath],  soundAct];NSLog(@"%@\n", path);//声明一个系统声音标识符SystemSoundID soundID;//转换字符类型变量的路径文件为URL连接  NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; //使用AudioToolbox框架提供的创建声音服务,定义音频的路径和创建声音标识符  AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);//播放指定的音频标识符AudioServicesPlaySystemSound(soundID);[filePath release];}//获取手指的触摸事件方法,- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {//touches参数获得事件,定制事件中的任何触摸对象UITouch *touch = [touches anyObject];//定义图形位置变量对象的坐标,locationInView表示视图中触摸点的坐标currentTouch = [touch locationInView:self.view];[UIImageView beginAnimations:nil context:NULL];[UIImageView setAnimationDuration:1.0];UFO.center=CGPointMake(currentTouch.x, currentTouch.y);[UIImageView commitAnimations];soundFile = [NSString stringWithFormat:@"/Sound.wav"];[self playSound: soundFile];}


原创粉丝点击