【问题标题】:Try to move UIImageView in screen with touchesMoved尝试使用 touchesMoved 在屏幕中移动 UIImageView
【发布时间】:2015-01-29 20:03:15
【问题描述】:

我尝试移动 2 个 UIImageView。

当我拖动时,应该只移动一张图片。

我试图完成的是当我触摸并拖动 UIImageView 时移动 UIImageView。

这是我的 viewDidLoad :

-(void)viewDidLoad 
{
 [super viewDidLoad];
  myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

 [myImageView setImage:[UIImage imageNamed:@"image1.png"]];

 [self.view addSubview:myImageView];


 myImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200,100)];

 [myImageView1 setImage:[UIImage imageNamed:@"image2"]];

 [self.view addSubview:myImageView1]; 
}

这是我的 touchesMoved 方法:

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 // get touch event
 UITouch *touch = [[event allTouches] anyObject];

 CGPoint touchLocation = [touch locationInView:self.view];

     for (UIImageView *image in [self.view subviews])
    {
       if ([touch view] == image)
       {
          // move the image view
          image.center = touchLocation;
       }

   }
}

在这一行我没有得到图像视图

for (UIImageView *image in [self.view subviews])

可以获取当前触摸的 UIIamgeView 吗?

【问题讨论】:

  • 这还不是问题。您需要告诉我们更多信息,例如: 1. 您想要完成什么。 2. 目前的结果是什么?我猜您正在尝试仅移动包含触摸的 UIImageView,但您需要更具体并提供更多详细信息。
  • 感谢我编辑帖子,并添加更多信息。
  • 你还没有真正告诉我们出了什么问题。
  • 怎么了??你的意思是你拖两张图片而不是一张?
  • 我现在不能移动图像,我尝试移动一个图像每次用户平移屏幕。

标签: objective-c touchesmoved


【解决方案1】:

您可以使用一些方法来拖动 UIImage(或其他界面元素):

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

我建议您下载并了解示例 Apple 应用程序,该应用程序旨在使用 UIKit 和 Core Animation 说明绘图、触摸处理和动画。就是这样:https://developer.apple.com/library/ios/samplecode/MoveMe/Introduction/Intro.html

希望对您有所帮助。

【讨论】:

  • 我的问题是如何获取当前的 UIIimageview 触摸。
  • Roel你需要实现手势识别,请阅读以下教程:raywenderlich.com/6567/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
相关资源
最近更新 更多