OS X Mavericks に XCode 5.0.1 をインストールしました。今、私は状況を説明しようとします。ストーリーボードに 1 つのビューがあり、その中に 384 個の UIImageView があります。それらはすべて IBOutletCollection にまとめられています。アイデアは次のとおりです。ループ内で、各 UIImageView のランダムな順序で 1 つまたは他の画像を割り当て、特定の方向に (ランダムな順序で) 回転させます。シミュレーターでアプリを実行すると、すぐに閉じられ、ヘッダーのようにエラーが発生します。何が問題なのか理解してください。.h ファイルのソース コードは次のとおりです。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
int i, irn, rn;
NSArray *clonedArray;
}
@property (nonatomic, retain) IBOutletCollection(UIImageView) NSArray *imageCollection;
-(void)didSwipe:(UISwipeGestureRecognizer *)sender;
@end
および.mファイルの:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)setImageViewsImages {
[self createRandoms];
for (i = 0; i < 384; i++) {
if (irn == 1) {
UIImageView *currentImage = [UIImageView alloc];
[currentImage setImage:[UIImage imageNamed:@"1.png"]];
if (rn == 1) {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(0);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set");
} else {
switch (rn) {
case 2: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 0.5);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
case 3: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
case 4: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 1.5);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
}
}
currentImage.image = nil;
}
if (irn == 2) {
UIImageView *currentImage = [UIImageView alloc];
[currentImage setImage:[UIImage imageNamed:@"2.png"]];
if (rn == 1) {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(0);
currentImage.transform = imageRotation;
[[self.imageCollection objectAtIndex:i] setObject:currentImage atIndex:i];
NSLog (@"image set");
} else {
switch (rn) {
case 2: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 0.5);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
case 3: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
case 4: {
CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 1.5);
currentImage.transform = imageRotation;
[[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
[[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
NSLog (@"image set"); }
break;
}
}
currentImage.image = nil;
}
}
}
-(void)createRandoms {
rn = (arc4random() %4) + 1;
irn = (arc4random() %2) + 1;
}
-(void)viewDidLoad {
[super viewDidLoad];
[self setImageViewsImages];
[self createRandoms];
clonedArray = [[NSArray alloc] initWithArray:self.imageCollection];
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(didSwipe:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:upSwipe];
UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(didSwipe:)];
downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:downSwipe];
}
-(void)didSwipe:(UISwipeGestureRecognizer *)sender {
UISwipeGestureRecognizerDirection direction = sender.direction;
switch (direction) {
case UISwipeGestureRecognizerDirectionUp:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
break;
case UISwipeGestureRecognizerDirectionDown:
[self createRandoms];
[self setImageViewsImages];
break;
case UISwipeGestureRecognizerDirectionRight:
break;
case UISwipeGestureRecognizerDirectionLeft:
break;
}
}
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Picture cannot be saved. \nTry again."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Close", nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Picture saved successfully."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Close", nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
私はObjective-Cのプログラミングとコーディングが初めてなので、愚かなコード、エラー、下手な英語を許してください:)
ありがとう!