ios - 让图像沿着 x 轴跟随您的手指,但具有固定的 y 坐标

标签 ios objective-c cocoa-touch

我正在使用 touchesMoved 方法让图像沿着屏幕底部“跟随”某人的手指。因此图像将跟随手指的 x 位置,但忽略 y 位置并保持垂直固定在底部(但不是水平固定)。有什么方法可以实现吗?

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // create basket image that will be shown on the screen
    basketView = [[UIImageView alloc]
    initWithImage:[UIImage imageNamed:@"bucket.png"]];
    basketView.frame = CGRectMake(130.0, 412.0, 50.0, 50.0);
    [self.view addSubview:basketView];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get current touch location
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint point = [touch locationInView:self.view];
    // update location of the image
    basketView.center = point;
}

最佳答案

保持y位置,改变x

basketView.center = CGPointMake(point.x, basketView.center.y);

关于ios - 让图像沿着 x 轴跟随您的手指,但具有固定的 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631728/

相关文章:

objective-c - Paint App 的软刷?

objective-c - 在不明确定义 CGRect 的情况下将帧缩小 80%

iphone - 使用 AudioServices 在 iPhone 上停止播放声音样本

cocoa-touch - 如何将NSValue保存到磁盘?

ios - 适用于 iOS 的 Ionic 构建 - ionic-native/core 版本问题

ios - Swift Annotation CallOut 按钮选择

ios - 有时 NSManagedobject 对象在将对象从一个 Controller 传递到另一个 Controller 时会出错,有没有办法将对象作为值类型 objC 传递

ios - 将矩阵转换为 UIImage

ios - 我们如何使用MFMailComposeViewCotroller发送设备信息

iphone - 如何手动删除 UITableView 中的一行?