我正在开发一个数学应用程序,让用户将硬币(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];
}
}
谢谢,
Ryan W
答案 0 :(得分:1)
前段时间我做了类似的事情。我只使用了触摸处理。 可能是示例代码会有所帮助。
创建新的空项目。
添加到项目“coin.png”(PNG图像32x32,如下所示)。
在项目中创建名为ViewController的新文件作为UIViewController的子类,不带xib。
在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
运行项目。并尝试将一个硬币拖放到黄色框内和外面