简体   繁体   English

声明/初始化CGPoints数组时遇到问题

[英]Having trouble declaring/initializing array of CGPoints

Like most of my problems recently, i think it's a syntax problem. 像最近我的大多数问题一样,我认为这是语法问题。 I know what i want to do but i don't know how to write it. 我知道我想做什么,但我不知道该怎么写。 I am trying to create a Jigsaw-type application. 我正在尝试创建一个拼图类型的应用程序。 The CGPoints represent the center point of where each image should be. CGPoints代表每个图像应位于的中心点。

  1. I have created 40 images and put them into an array. 我已经创建了40张图像,并将它们放入一个数组中。
  2. I want to create 40 CGPoints and put all these points in an array. 我想创建40个CGPoint,并将所有这些点放入一个数组中。
  3. The image at images[0] has a corresponding center at cgpoints[0] - or rather this is where the image should be placed in the jigsaw. images [0]处的图像在cgpoints [0]处具有一个相应的中心-或应将其放置在竖锯中。

I'm having trouble declaring/initializing the array of CGPoints though. 我在声明/初始化CGPoints数组时遇到了麻烦。 Below is the code: 下面是代码:

.h file .h文件

#import <UIKit/UIKit.h>

@interface DSAAssignmentViewController2 : UIViewController

@property (weak, nonatomic) NSString *passedData;
@property (weak, nonatomic) IBOutlet UILabel *lblDisplayPreviousInput;
@property (weak, nonatomic) IBOutlet UIImageView *imgOriginal;
@property (weak, nonatomic) IBOutlet UIImageView *img1;
@property (weak, nonatomic) IBOutlet UIImageView *img2;
@property (weak, nonatomic) IBOutlet UIImageView *img3;
@property (weak, nonatomic) IBOutlet UIImageView *img4;
@property (weak, nonatomic) IBOutlet UIImageView *img5;
@property (weak, nonatomic) IBOutlet UIImageView *img6;
@property (weak, nonatomic) IBOutlet UIImageView *img7;
@property (weak, nonatomic) IBOutlet UIImageView *img8;
@property (weak, nonatomic) IBOutlet UIImageView *img9;
@property (weak, nonatomic) IBOutlet UIImageView *img10;
@property (weak, nonatomic) IBOutlet UIImageView *img11;
@property (weak, nonatomic) IBOutlet UIImageView *img12;
@property (weak, nonatomic) IBOutlet UIImageView *img13;
@property (weak, nonatomic) IBOutlet UIImageView *img14;
@property (weak, nonatomic) IBOutlet UIImageView *img15;
@property (weak, nonatomic) IBOutlet UIImageView *img16;
@property (weak, nonatomic) IBOutlet UIImageView *img17;
@property (weak, nonatomic) IBOutlet UIImageView *img18;
@property (weak, nonatomic) IBOutlet UIImageView *img19;
@property (weak, nonatomic) IBOutlet UIImageView *img20;
@property (weak, nonatomic) IBOutlet UIImageView *img21;
@property (weak, nonatomic) IBOutlet UIImageView *img22;
@property (weak, nonatomic) IBOutlet UIImageView *img23;
@property (weak, nonatomic) IBOutlet UIImageView *img24;
@property (weak, nonatomic) IBOutlet UIImageView *img25;
@property (weak, nonatomic) IBOutlet UIImageView *img26;
@property (weak, nonatomic) IBOutlet UIImageView *img27;
@property (weak, nonatomic) IBOutlet UIImageView *img28;
@property (weak, nonatomic) IBOutlet UIImageView *img29;
@property (weak, nonatomic) IBOutlet UIImageView *img30;
@property (weak, nonatomic) IBOutlet UIImageView *img31;
@property (weak, nonatomic) IBOutlet UIImageView *img32;
@property (weak, nonatomic) IBOutlet UIImageView *img33;
@property (weak, nonatomic) IBOutlet UIImageView *img34;
@property (weak, nonatomic) IBOutlet UIImageView *img35;
@property (weak, nonatomic) IBOutlet UIImageView *img36;
@property (weak, nonatomic) IBOutlet UIImageView *img37;
@property (weak, nonatomic) IBOutlet UIImageView *img38;
@property (weak, nonatomic) IBOutlet UIImageView *img39;
@property (weak, nonatomic) IBOutlet UIImageView *img40;
@property (strong, nonatomic) NSArray *imageViews;

@property CGPoint *holder1;
@property CGPoint *holder2;
@property CGPoint *holder3;
@property CGPoint *holder4;
@property CGPoint *holder5;
@property CGPoint *holder6;
@property CGPoint *holder7;
@property CGPoint *holder8;
@property (strong, nonatomic) NSMutableArray *holders;

@end

.m file .m文件

#import "DSAAssignmentViewController2.h"

@interface DSAAssignmentViewController2 ()

@end

@implementation DSAAssignmentViewController2
@synthesize lblDisplayPreviousInput;
@synthesize imgOriginal;
@synthesize img1;
@synthesize img2;
@synthesize img3;
@synthesize img4;
@synthesize img5;
@synthesize img6;
@synthesize img7;
@synthesize img8;
@synthesize img9;
@synthesize img10;
@synthesize img11;
@synthesize img12;
@synthesize img13;
@synthesize img14;
@synthesize img15;
@synthesize img16;
@synthesize img17;
@synthesize img18;
@synthesize img19;
@synthesize img20;
@synthesize img21;
@synthesize img22;
@synthesize img23;
@synthesize img24;
@synthesize img25;
@synthesize img26;
@synthesize img27;
@synthesize img28;
@synthesize img29;
@synthesize img30;
@synthesize img31;
@synthesize img32;
@synthesize img33;
@synthesize img34;
@synthesize img35;
@synthesize img36;
@synthesize img37;
@synthesize img38;
@synthesize img39;
@synthesize img40;
@synthesize passedData;
@synthesize imageViews;

@synthesize holder1;
@synthesize holder2;
@synthesize holder3;
@synthesize holder4;
@synthesize holder5;
@synthesize holder6;
@synthesize holder7;
@synthesize holder8;
@synthesize holders;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // make jigsaw piece holders
    //create 8 points where the jigsaw pieces (images) will be placed
    CGPoint holder1 = CGPointMake(60, 644);
    CGPoint holder2 = CGPointMake(140, 644);
    CGPoint holder3 = CGPointMake(220, 644);
    CGPoint holder4 = CGPointMake(300, 644);
    CGPoint holder5 = CGPointMake(380, 644);
    CGPoint holder6 = CGPointMake(460, 644);
    CGPoint holder7 = CGPointMake(540, 644);
    CGPoint holder8 = CGPointMake(620, 644);

    // set up array of cgpoints
    holders = [NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint: holder1],[NSValue valueWithCGPoint: holder2], [NSValue valueWithCGPoint: holder3], [NSValue valueWithCGPoint: holder4], [NSValue valueWithCGPoint: holder5], [NSValue valueWithCGPoint: holder6], [NSValue valueWithCGPoint: holder7], [NSValue valueWithCGPoint: holder8], nil];

    // set up array of images
    imageViews = [NSArray arrayWithObjects:img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18, img19, img20, img21, img22, img23, img24, img25, img26, img27, img28, img29, img30, img31, img32, img33, img34, img35, img36, img37, img38, img39, img40, nil];

    NSString *welcomeMessage = [NSString stringWithFormat:@"Welcome %@", passedData];
    UIAlertView *alert = [[UIAlertView alloc]   //show alert box with option to play or exit
                          initWithTitle: welcomeMessage 
                          message:@"Once you press \"Play\" the timer will start. Good luck!" 
                          delegate:self 
                          cancelButtonTitle:@"I want out!" 
                          otherButtonTitles:@"Play",nil];
    [alert show];
    NSLog(@"x-coord: %f, y-coord: %f", holder1.x, holder1.y);
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    UIView *touchedView = [touch view];
    if ([imageViews indexOfObject:touchedView] != NSNotFound) {
        // not not found means found!
        [self animateFirstTouch:touchedView withLocation:location];
    } else {
        return;
    }

}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesBegan:touches withEvent:event];
}


-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    UIView *touchedView = [touch view];
    [self animateReleaseTouch:touchedView withLocation:location];

}



- (void)viewDidUnload
{
    [self setLblDisplayPreviousInput:nil];
    [self setImgOriginal:nil];
    [self setImg1:nil];
    [self setImg2:nil];
    [self setImg3:nil];
    [self setImg4:nil];
    [self setImg5:nil];
    [self setImg6:nil];
    [self setImg7:nil];
    [self setImg8:nil];
    [self setImg9:nil];
    [self setImg10:nil];
    [self setImg11:nil];
    [self setImg12:nil];
    [self setImg13:nil];
    [self setImg14:nil];
    [self setImg15:nil];
    [self setImg16:nil];
    [self setImg17:nil];
    [self setImg18:nil];
    [self setImg19:nil];
    [self setImg20:nil];
    [self setImg21:nil];
    [self setImg22:nil];
    [self setImg23:nil];
    [self setImg24:nil];
    [self setImg25:nil];
    [self setImg26:nil];
    [self setImg27:nil];
    [self setImg28:nil];
    [self setImg29:nil];
    [self setImg30:nil];
    [self setImg31:nil];
    [self setImg32:nil];
    [self setImg33:nil];
    [self setImg34:nil];
    [self setImg35:nil];
    [self setImg36:nil];
    [self setImg37:nil];
    [self setImg38:nil];
    [self setImg39:nil];
    [self setImg40:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location {
    image.center = location;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    image.transform = CGAffineTransformMakeScale(1.25, 1.25);  
    [UIView commitAnimations];   
}

-(void) animateReleaseTouch:(UIImageView *)image withLocation:(CGPoint)location {
    image.center = location;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    image.transform = CGAffineTransformMakeScale(1.0, 1.0);  
    [UIView commitAnimations];   
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

I can't tell if its all a bit of a mess or not. 我不知道是不是一团糟。 As you can see i've started with only 8 "holders", these are the CGPoints that represent the center of where the jigsaw pieces should be placed. 如您所见,我最初只有8个“持有人”,这些是CGPoints,它们代表放置拼图的位置的中心。

borrrden is correct. borrrden是正确的。 You've created a whole bunch of CGPoint pointer variables by declaring them as properties and synthesizing them. 通过将它们声明为属性并进行综合,已经创建了一大堆CGPoint指针变量。 But then in your -viewDidLoad method you're declaring CGPoint structs--not CGPoint pointers--all with the same name as the properties you synthesized. 但是,然后在您的-viewDidLoad方法中,您要声明CGPoint结构(而不​​是CGPoint指针),它们都具有与您合成的属性相同的名称。

You don't need the holderN properties at all, to judge from the code. 从代码中判断,您根本不需要holderN属性。 In fact, you don't even need the holderN variables in the -viewDidLoad method. 实际上,您甚至不需要-viewDidLoad方法中的holderN变量。 You could easily get away with something like this: 您可以轻松地摆脱类似这样的东西:

- ( void )viewDidLoad
{
    holders = [ NSMutableArray arrayWithObjects:
                               [ NSValue valueWithCGPoint: CGPointMake(60, 644) ],
                               ...,
                               [ NSValue valueWithCGPoint: CGPointMake(N, N) ],
                               nil ];
    ...
}

Hard-coding the point coordinates is poor design, though. 但是,对点坐标进行硬编码的设计较差。 Your best option is to put the coordinates in a plist, which you can load on demand and stuff into an array. 最好的选择是将坐标放在plist中,您可以根据需要将其加载并填充到数组中。 This'll mean tweaking the coordinates is just a matter of editing the plist. 这意味着调整坐标只是编辑plist的问题。 No recompiling required. 无需重新编译。

之所以会在注释中得到该消息,是因为您在viewDidLoad的开头声明了与属性同名的变量。

NSPoint不是对象,因此您必须使用C数组: NSPoint points[SIZE]或使用NSValue容器: [NSValue valueWithCGPoint: point]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM