ページ

2011年6月25日土曜日

Xcode : Multiple Image を Drag して動かす





Drag_MultipleImageViewController.h

#import <UIKit/UIKit.h>

@interface Drag_MultipleImageViewController : UIViewController {

 IBOutlet UIImageView *cookie1;
 IBOutlet UIImageView *cookie2;
 IBOutlet UIImageView *cookie3;
 IBOutlet UIImageView *cookie4;
 IBOutlet UIImageView *cookieMonster;
 
 CGPoint imagePositon;
 NSTimer *timer;

}

-(void)imageMove;

@end

Drag_MultipleImageViewController.m

#import "Drag_MultipleImageViewController.h"

@implementation Drag_MultipleImageViewController

-(void)imageMove{
 cookieMonster.center = CGPointMake(cookieMonster.center.x + imagePositon.x, 
             cookieMonster.center.y + imagePositon.y );
 if(cookieMonster.center.x > 300 || cookieMonster.center.x < 20)
  imagePositon.x = - imagePositon.x;
 if(cookieMonster.center.y > 440 || cookieMonster.center.y < 20)
  imagePositon.y = - imagePositon.y;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch = [[event allTouches] anyObject];
 CGPoint location = [touch locationInView:self.view];
 
 if ([touch view] == cookie1) {
  cookie1.center = location;
 }else if ([touch view] == cookie2) {
  cookie2.center = location;
 }else if ([touch view] == cookie3) {
  cookie3.center = location;
 }else if ([touch view] == cookie4) {
  cookie4.center = location;
 }
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
 imagePositon = CGPointMake(10, 10);
 timer = [NSTimer scheduledTimerWithTimeInterval:0.05 
            target:self 
             selector:@selector(imageMove) 
             userInfo:nil
           repeats:YES];
}

- (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


0 件のコメント:

コメントを投稿