gpt4 book ai didi

iOS:当拖动到指定区域时如何仅确定一次按钮的位置

转载 作者:行者123 更新时间:2023-11-29 03:30:55 25 4
gpt4 key购买 nike

我正在开发一个数学应用程序,让用户将硬币(UIButton)拖动到黄色框(UIImageView)中,黄色框中将显示总金额。在对堆栈溢出的一些帮助和我自己的发现之后,我想出了下面的方法。基本上问题是,每次我稍微移动硬币时,显示黄色框中有多少钱的标签都会增加(硬币在黄色框中时的每次轻微移动都会增加总金额)。

当硬币完全进入黄色盒子时,我试图仅增加黄色盒子的总量一次。如果硬币被拖出黄色盒子,则总金额将被减去。我目前正在所有硬币上使用触摸拖动。我不确定我哪里出错了。任何建议将不胜感激。

-(IBAction)dragged_out:(id)sender withEvent: (UIEvent *) event
{
UIButton *selected = (UIButton *)sender;
selected.center = [[[event allTouches] anyObject] locationInView:self.view];


if(CGRectContainsRect(yellowBox.frame, selected.frame))
{
amountYellowBox += 5;
totalAmountYellowBox.text =[NSString stringWithFormat:@"Current Amount: %d",amountYellowBox];
}
}

谢谢

瑞安W

最佳答案

前段时间我也做过类似的事情。我只使用了触摸处理。示例代码可能会有所帮助。

创建新的空项目。

添加到项目“coin.png”(PNG 图像 32x32,如下所示)。

coin image

在项目中创建名为 ViewController 的新文件,作为不带 xib 的 UIViewController 的子类。

在 AppDelegate.m 文件中删除所有并添加以下代码:

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
ViewController *viewController = [ViewController new];
[_window setRootViewController:viewController];
return YES;
}

@end

在 ViewController.m 文件中删除所有内容并添加以下代码:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) int amount;
@property (nonatomic, strong) UILabel *count;
@property (nonatomic, strong) UIView *coinBox;

@end

@implementation ViewController


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]])
{
[[touch view] setCenter:[touch locationInView:[[touch view] superview]]];
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]])
{
[[touch view] setCenter:[touch locationInView:[[touch view] superview]]];
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]])
{
if(CGRectContainsRect(_coinBox.frame, [touch view].frame))
{
_amount += 5;
_count.text =[NSString stringWithFormat:@"Amount: %d",_amount];
[[touch view] setUserInteractionEnabled:NO];
}
}
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self createCoinBox];
[self createCoins];
}

- (void)createCoinBox
{
_coinBox = [[UIView alloc] initWithFrame:CGRectMake(120.0, 200.0, 80.0, 80.0)];
[_coinBox setBackgroundColor:[UIColor yellowColor]];
[[self view] addSubview:_coinBox];
_count = [[UILabel alloc] initWithFrame:CGRectMake(200.0, 200.0, 120.0, 32.0)];
[_count setBackgroundColor:[UIColor grayColor]];
[_count setText:@"Amount: 0"];
[[self view] addSubview:_count];
}

- (void)createCoins
{
for (int i = 0; i < 8; i++)
{
UIImageView *coin = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coin"]];
CGRect coinFrame = CGRectMake(40.0 * i, 40.0, 32.0, 32.0);
[coin setFrame:coinFrame];
[coin setUserInteractionEnabled:YES];
[[self view] addSubview:coin];
}
}

@end

运行项目。并尝试将硬币拖放到黄色框内和黄色框外

关于iOS:当拖动到指定区域时如何仅确定一次按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19788580/

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