0

在屏幕上移动手指时,我在视图中添加图像时遇到问题。

目前它正在多次添加图像,但将它们挤压得太近,并没有真正跟随我的触摸。

编辑:

我想要的是:

在拍摄或选择图像之后,用户可以从列表中选择另一个图像。我希望用户在视图中触摸并移动他们的手指,所选图像将出现在他们拖动手指的位置,而不会在每个位置重叠。

这有效:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
     currentTouch = [touch locationInView:self.view];

     CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 80.0f, 80.0f);
     myImage = [[UIImageView alloc] initWithFrame:myImageRect];
     [myImage setImage:[UIImage imageNamed:@"dot.png"]];
     [self.view addSubview:myImage];
     [myImage release];
}

新问题:如何在其中添加空格以使图像在视图上绘制时不那么紧贴?

4

3 回答 3

0

您可能想更多地解释您的问题,您到底想达到什么目的!如果您不希望图像重叠,可以试试这个!

UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];
prev_touchPoint = [touch previousLocationInView:imageView];

if (ABS(touchPoint.x - prev_touchPoint.x) > 80 
    || ABS(touchPoint.y - prev_touchPoint.y) > 80) {

   _aImageView = [[UIImageView alloc] initWithImage:aImage];
   _aImageView.multipleTouchEnabled = YES;
   _aImageView.userInteractionEnabled = YES;
  [_aImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 80.0, 80.0)];
  [imageView addSubview:_aImageView];
  [_aImageView release];
}
于 2012-11-06T04:15:23.263 回答
0

对不起,因为我在公司,我不能发布太大的数据(代码)。挤压是因为您没有检查接触点与最后一个接触点的距离。检查一个点是否在视图中:bool CGRectContainsPoint (CGRect rect,CGPoint point);我的意思是记住touchesBegan:. 如果新的触摸touchesMomved:大于图像的宽度或左侧,则更新它。并将添加图像视图放在一个方法中并调用它 use - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

于 2012-11-06T04:57:26.520 回答
0

你也可以使用 UISwipeGestureRecognizer 代替 touchesMoved 方法,同时在屏幕上滑动。在 viewDidload:method 中,

UISwipeGestureRecognizer *swipeup = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipedone:)];
swipeup.direction = UISwipeGestureRecognizerDirectionUp;
swipeup.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:swipeup];

方法定义:

-(IBAction)swipedone:(UISwipeGestureRecognizer*)recognizer
{
    NSLog(@"swiped");
    UIImageView* _aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    _aImageView.frame = CGRectMake(10, 10, 100, 100);
    _aImageView.multipleTouchEnabled = YES;
    _aImageView.userInteractionEnabled = YES;
    CGPoint point = [recognizer locationInView:recognizer.view];
    [_aImageView setFrame:CGRectMake(point.x, point.y, 80.0, 80.0)];
    [self.view addSubview:_aImageView];
    [_aImageView release];
}

目前我正在使用此代码向上滑动。我认为它会正常工作。一次尝试。

于 2012-11-06T06:45:32.677 回答