带有 CGRectIntersectsRect 的 UIImageView 子类 - 帮助!

发布于 2024-09-03 18:53:20 字数 2357 浏览 0 评论 0原文

这个问题的标题应该是不言自明的。我正在制作一个涉及多个具有相同目的的 UIImageView 的应用程序。它们只是尺寸不同。不管怎样,我认为最好的解决方案是创建 UIImageView 子类,链接 IB 中的子类,然后从那里开始工作。我的代码应该更好地解释这一点 -



#define kPausedStatePaused 1
#define kPausedStatePlay 2

#import "Game.h"
#import "ScoreSystem.h"

@interface Doctor : UIImageView
{
}
@end

@interface Ground : UIImageView
{
}
@end

@interface Wall : UIImageView
{
}
@end

@interface Electric_Wire : UIImageView
{
}
@end


@implementation Game

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

- (IBAction)pause {
 UIAlertView *pause = [[UIAlertView alloc] initWithTitle:@"Pause" message:nil delegate:self cancelButtonTitle:@"Quit" otherButtonTitles:@"Play", nil];
 [pause show];
 [pause release];
 pauseint = kPausedStatePaused;
}

- (void)viewDidAppear {
 pauseint = kPausedStatePlay;
}

- (void)loop {
 Doctor *doctorview;
 Ground *groundview;
 if (CGRectIntersectsRect(doctorview, groundview)) {

 }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if ([alertView.title isEqual:@"Pause"]) {
  if(buttonIndex == 0)
   [self dismissModalViewControllerAnimated:YES];
  pauseint = kPausedStatePlay;
 }
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

不出所料,Xcode 给了我一个“CGRectIntersectsRect 的类型不兼容”错误。

The title of this question should be pretty self explanatory. I am making an app that involves multiple UIImageViews that serve the same purpose. They are merely different sizes. Anyway, I decided that the best solution was to make UIImageView subclasses, link the subcalsses in IB, and work from there. My code should explain this better -



#define kPausedStatePaused 1
#define kPausedStatePlay 2

#import "Game.h"
#import "ScoreSystem.h"

@interface Doctor : UIImageView
{
}
@end

@interface Ground : UIImageView
{
}
@end

@interface Wall : UIImageView
{
}
@end

@interface Electric_Wire : UIImageView
{
}
@end


@implementation Game

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

- (IBAction)pause {
 UIAlertView *pause = [[UIAlertView alloc] initWithTitle:@"Pause" message:nil delegate:self cancelButtonTitle:@"Quit" otherButtonTitles:@"Play", nil];
 [pause show];
 [pause release];
 pauseint = kPausedStatePaused;
}

- (void)viewDidAppear {
 pauseint = kPausedStatePlay;
}

- (void)loop {
 Doctor *doctorview;
 Ground *groundview;
 if (CGRectIntersectsRect(doctorview, groundview)) {

 }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 if ([alertView.title isEqual:@"Pause"]) {
  if(buttonIndex == 0)
   [self dismissModalViewControllerAnimated:YES];
  pauseint = kPausedStatePlay;
 }
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

Unsurprisingly, Xcode gave me an "incompatible type for CGRectIntersectsRect" error.

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

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

发布评论

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

评论(1

愿得七秒忆 2024-09-10 18:53:20

您必须将视图的框架而不是视图本身传递给该函数:

CGRectIntersectsRect(doctorview.frame, groundview.frame)

You must pass view's frame, not the view's themselves to that function:

CGRectIntersectsRect(doctorview.frame, groundview.frame)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文