我在屏幕上移动手指时在视图上添加图像问题。
目前,它正在多次添加图像,但将它们挤压在一起,而不是真正跟随我的触摸。
编辑:
我想要的:
拍摄或选择图像后,用户可以从列表中选择另一个图像。我希望用户触摸并将手指移动到视图中,并且所选图像将出现在他们拖动手指而不重叠的地方。
这有效:
-(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];
}
新问题:如何在此添加空间,以使图像在视图上绘制时不会那么贴合?
您可能需要更多地解释您的问题,您到底想实现什么!如果您不希望图像重叠,而是可以尝试!
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];
}
我很抱歉,因为我在公司里,我不能发布太大数据(代码)。挤压是因为您尚未使用最后一个接触点检查接触点的距离。检查一个点是否在视图中:bool CGRectContainsPoint (CGRect rect,CGPoint point);
我的意思是记住touchesBegan:
中的接触点。如果touchesMomved:
中的新触摸大于图像的宽度或向左,则更新它。并将添加图像视图放入方法中,然后将其称为使用- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
。
您也可以在下面使用UisWipeGeSturerEcognizer而不是touchesmasged方法,而在屏幕上刷卡。
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];
}
目前,我正在使用此代码进行滑动。我认为它会很好地工作。