0

我正在开发一个数学应用程序,它允许用户将硬币(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

4

1 回答 1

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

运行项目。并尝试在黄色盒子内外拖放一枚硬币

于 2013-11-06T13:40:58.340 回答