gpt4 book ai didi

objective-c - 如何使用 iPhone SDK 只拖动一张图片

转载 作者:行者123 更新时间:2023-11-28 19:24:33 24 4
gpt4 key购买 nike

我想创建一个可以拍摄两张图片的小应用程序,我只想让图片可以拖动。经过研究,我找到了这个解决方案:

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

UITouch *touch = [[ event allTouches] anyObject];

image.alpha = 0.7;

if([touch view] == image){
CGPoint location = [touch locationInView:self.view];
image.center = location;
}

它有效,但问题是图像可以从其中心拖动,我不希望这样。所以我找到了另一个解决方案:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
// Retrieve the touch point
CGPoint pt = [[touches anyObject] locationInView:self.view];
startLocation = pt;
[[self view] bringSubviewToFront:self.view];

}


- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point

CGPoint pt = [[touches anyObject] locationInView:self.view];
frame = [self.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self.view setFrame:frame];
}

效果很好,但是当我添加另一张图片时, View 的所有图片都可以同时拖动。我是 iPhone 开发的新手,我不知道如何才能让图像变得可拖动。

最佳答案

你可以在 touchesBegan: 方法中使用 if 条件,看这个-

-(void)ViewDidLoad
{
//Your First Image View
UIImageView *imageView1 = [UIImageView alloc]initWithFrame:CGRectMake(150.0, 100.0, 30.0, 30.0)];
[imageView1 setImage:[UIImage imageNamed:@"anyImage.png"]];
[imageView1 setUserInteractionEnabled:YES];
[self.view addSubview:imageView1];

//Your Second Image View
UIImageView *imageView2 = [UIImageView alloc]initWithFrame:CGRectMake(150.0, 200.0, 30.0, 30.0)];
[imageView2 setImage:[UIImage imageNamed:@"anyImage.png"]];
[imageView2 setUserInteractionEnabled:YES];
[self.view addSubview:imageView2];
}

//Now Use Touch begin methods with condition like this

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == imageView1)
{
CGPoint pt = [[touches anyObject] locationInView:imageView1];
startLocation = pt;
}

if([touch view] == imageView2)
{
CGPoint pt = [[touches anyObject] locationInView:imageView2];
startLocation = pt;
}
}

//Use same thing with touch move methods...
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == imageView1)
{
CGPoint pt = [[touches anyObject] previousLocationInView:imageView1];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(imageView1.center.x + dx, imageView1.center.y + dy);
imageView1.center = newCenter;
}

if([touch view] == imageView2)
{
CGPoint pt = [[touches anyObject] previousLocationInView:imageView2];
CGFloat dx = pt.x - startLocation.x;
CGFloat dy = pt.y - startLocation.y;
CGPoint newCenter = CGPointMake(imageView2.center.x + dx, imageView2.center.y + dy);
imageView2.center = newCenter;
}
}

这两个图像只有在您触摸并移动它时才会移动。希望我的教程对您有所帮助。

关于objective-c - 如何使用 iPhone SDK 只拖动一张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com