查找正在设置UIimageview框架的位置

时间:2014-04-03 04:04:05

标签: objective-c

我正在使用Xcode 5中的iPad应用程序进行拼图游戏。我在故事板中创建了布局,为9个帧中的每一个添加了uiimageviews,这些帧将保存将成为拼图的图像的一部分,并将它们放置在视图控制器上。其他人已经从游戏中删除了故事板,我需要回去完成没有它的拼图。我遇到的最大问题是框架的位置似乎被设置在视图controller.h或.m类之外的某处,我找不到它。我可以在touchesmoved方法中使用image1.center更改片段的位置,但不能在viewdidload中更改。我一直在试图追踪这几个小时的位置并且没有运气。在viewdidload,viewwillappear,viewdidappear等中设置中心的任何移动都不会按预期更改位置。

任何人都可以指导我在视图加载后更改image1.center但没有任何用户输入,或者告诉我如何跟踪视图中发生的所有更改?我不认为看到我的代码会对这个问题有所帮助,因为它在我看来的课程中似乎没有改变,但我会发布什么有效,什么没有。我只发布了触及uiimageview(1-9)的部分

来自.h

@implementation PuzzleViewController

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

@interface PuzzleViewController : UIViewController
{
    UIImageView *image1;
    UIImageView *image2;
    UIImageView *image3;
    UIImageView *image4;
    UIImageView *image5;
    UIImageView *image6;
    UIImageView *image7;
    UIImageView *image8;
    UIImageView *image9;
    UIImage *puzzlePic;

}

来自.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set up buttons to accept user input
    image1.userInteractionEnabled = true;
    image2.userInteractionEnabled = true;
    image3.userInteractionEnabled = true;
    image4.userInteractionEnabled = true;
    image5.userInteractionEnabled = true;
    image6.userInteractionEnabled = true;
    image7.userInteractionEnabled = true;
    image8.userInteractionEnabled = true;
    image9.userInteractionEnabled = true;

    // Do any additional setup after loading the view from its nib.
    puzzlePic = [LLFileManager getImage:@"star7.gif" folder:@"animation_images"];

    [self placeImage];

}

// handle touches within images to move
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if([touch view] == image1){
        image1.center = touchLocation;
    }
    else if([touch view] == image2){
        image2.center = touchLocation;

    }
    else if([touch view] == image3){
        image3.center = touchLocation;
    }
    else if([touch view] == image4){
        image4.center = touchLocation;
    }
    else if([touch view] == image5){
        image5.center = touchLocation;
    }
    else if([touch view] == image6){
        image6.center = touchLocation;
    }
    else if([touch view] == image7){
        image7.center = touchLocation;
    }
    else if([touch view] == image8){
        image8.center = touchLocation;
    }
    else if([touch view] == image9){
        image9.center = touchLocation;
    }
}

// splits image into 9 equal parts, takes input of a ui image

-(NSArray *)splitImage9:(UIImage *)im{
    NSMutableArray *images = [NSMutableArray array];
    CGSize imageSize = im.size;
    CGFloat xPos = 0.0, yPos = 0.0;
    CGFloat width = imageSize.width/3;
    CGFloat height = imageSize.height/3;
    for (int y = 0; y < 3; y++) {
        xPos = 0.0;
        for (int x = 0; x < 3; x++) {

            CGRect rect = CGRectMake(xPos, yPos, width, height);
            CGImageRef cImage = CGImageCreateWithImageInRect([im CGImage],  rect);

            UIImage *dImage = [[UIImage alloc] initWithCGImage:cImage];
            [images addObject:dImage];
            xPos += width;
        }
        yPos += height;
    }
    return images;
}

// position pieces on board load

-(void)positionPieces{ // test, not working
    CGPoint piece = CGPointMake(50,100);
    image2.center =  piece;

}


// places the cut images into view placeholders
-(void)placeImage{
    NSArray *puzzleImage = [self splitImage9: puzzlePic];
    [image1 setImage:[puzzleImage objectAtIndex:0]];
    [image2 setImage:[puzzleImage objectAtIndex:1]];
    [image3 setImage:[puzzleImage objectAtIndex:2]];
    [image4 setImage:[puzzleImage objectAtIndex:3]];
    [image5 setImage:[puzzleImage objectAtIndex:4]];
    [image6 setImage:[puzzleImage objectAtIndex:5]];
    [image7 setImage:[puzzleImage objectAtIndex:6]];
    [image8 setImage:[puzzleImage objectAtIndex:7]];
    [image9 setImage:[puzzleImage objectAtIndex:8]];
}

1 个答案:

答案 0 :(得分:1)

  

我遇到的最大问题是框架的位置似乎设置在视图controller.h或.m类之外的某处,我无法找到它。

如果故事板消失了,如果没有添加代码将图像视图放入界面,那么界面应为空。你完全看到界面的原因必须是因为你不小心运行了一个过时的项目版本。

要解决这个问题,请按照我在此解释清理项目:

How to Empty Caches and Clean All Targets Xcode 4

当你再次运行项目时,你会发现界面现在是空的并且解开了谜团:框架有没有位置,因为有没有帧,因为根本没有没有图像视图。