在 TouchesBegan 上更改图像

发布于 2024-11-01 04:44:37 字数 670 浏览 4 评论 0原文

嘿,我试图在用户触摸 ImageView 时更改图像。目前 ImageView 具有图像“heart1.png”,因此当我触摸屏幕上的 ImageViewer 时,它将更改为“heart2.png”,这是我的代码,非常感谢帮助:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];
    if (location.x >= imageView.frame.origin.x && location.x <= imageView.frame.origin.x + imageView.frame.size.height && location.y >= imageView.frame.origin.y && location.y <= imageView.frame.origin.y + imageView.frame.size.height) {
        imageView.image = [UIImage imageNamed:@"heart2.png"];
    }    
}

Hey, I am trying to change the image when the user touches the ImageView. At the moment the ImageView has the image "heart1.png" so when i touch the ImageViewer on screen it will change to "heart2.png" this is my code, help is much appreciated:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];
    if (location.x >= imageView.frame.origin.x && location.x <= imageView.frame.origin.x + imageView.frame.size.height && location.y >= imageView.frame.origin.y && location.y <= imageView.frame.origin.y + imageView.frame.size.height) {
        imageView.image = [UIImage imageNamed:@"heart2.png"];
    }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞花火° 2024-11-08 04:44:37

这对我来说似乎是个问题:

if (location.x >= imageView.frame.origin.x && location.x <= imageView.frame.origin.x + imageView.frame.size. --> 高度 <--

可能应该是宽度:)

That looks like a problem to me:

if (location.x >= imageView.frame.origin.x && location.x <= imageView.frame.origin.x + imageView.frame.size.--> height <--

should be width there probably :)

帅冕 2024-11-08 04:44:37

与其胡乱检查边界矩形的坐标,为什么不通过使用界面构建在图像上放置一个自定义(即不可见)UIButton 来简化事情呢?然后,您可以将 touchesUpInside 连接到 IBAction 方法。这样你的代码中就不会出现尴尬的 if 语句,并且犯这样的错误的可能性也会减少。

Rather than fool around with checking coordinates against a bounding rect, why don't you simplify things by using interface building to place a custom (i.e. invisible) UIButton right over the image? Then you can hook up touchesUpInside to an IBAction method. Then there's no awkward if statements in your code and less chance of making mistakes like this.

雨夜星沙 2024-11-08 04:44:37

考虑使用 UIButton 代替。

[button setImage:[UIImage imageNamed:@"heart1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"heart2.png"] forState:UIControlStateHighlighted];

Consider using an UIButton instead.

[button setImage:[UIImage imageNamed:@"heart1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"heart2.png"] forState:UIControlStateHighlighted];
反差帅 2024-11-08 04:44:37

不需要找到view的位置,也不需要移动UIImageView。只需这样做:

-(void)viewDidLoad 
{
    // imageView is your UIImageView
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    [imageView setImage:[UIImage imageNamed:@"heart1.png"]];
    [imageView setUserInteractionEnabled:YES];

    // Make sure userInteractionEnable is set to be YES; otherwise the touch event will be happen on UIView not on ImageView;
   [self.view addSubview:imageView];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches]anyObject];
    if([touch view]==imageView)
        [imageView setImage:[UIImage imageNamed:@"heart2.png"]];
}

There is no need to find location of view, You don't have to do move UIImageView. Simply do this:

-(void)viewDidLoad 
{
    // imageView is your UIImageView
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    [imageView setImage:[UIImage imageNamed:@"heart1.png"]];
    [imageView setUserInteractionEnabled:YES];

    // Make sure userInteractionEnable is set to be YES; otherwise the touch event will be happen on UIView not on ImageView;
   [self.view addSubview:imageView];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches]anyObject];
    if([touch view]==imageView)
        [imageView setImage:[UIImage imageNamed:@"heart2.png"]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文