2011年6月14日火曜日

Xcode buttonでimageをMoveする





buttonMoveViewController.h
#import <UIKit/UIKit.h>

@interface buttonMoveViewController : UIViewController {

 IBOutlet UIImageView *pic;
}

@property(nonatomic, retain) UIImageView *pic;

-(IBAction)moveUp:(id)sender;
-(IBAction)moveDown:(id)sender;
-(IBAction)moveLeft:(id)sender;
-(IBAction)moveRight:(id)sender;

@end

buttonMoveViewController.m
#import "buttonMoveViewController.h"

@implementation buttonMoveViewController

@synthesize pic;

-(IBAction)moveUp:(id)sender{
 
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.5];
 CGPoint center = pic.center;
 
 if(center.y > 0){
  center.y -= 10;
  pic.transform = CGAffineTransformMakeScale(.35, 35);
  center.y -= 10;
  pic.transform = CGAffineTransformMakeScale(1, 1);
  pic.center = center;
 }
 
}

-(IBAction)moveDown:(id)sender{
 
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.5];
 CGPoint center = pic.center;
 
 if(center.y < 300){
  center.y += 10;
  pic.transform = CGAffineTransformMakeScale(.35, 35);
  center.y += 10;
  pic.transform = CGAffineTransformMakeScale(1, 1);
  pic.center = center;
 }
}

-(IBAction)moveLeft:(id)sender{
 
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.5];
 CGPoint center = pic.center;
 
 if(center.x > 0){
  center.x -= 10;
  pic.transform = CGAffineTransformMakeScale(.35, 35);
  center.x -= 10;
  pic.transform = CGAffineTransformMakeScale(1, 1);
  pic.center = center;
 }
 
}

-(IBAction)moveRight:(id)sender{
 
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.5];
 CGPoint center = pic.center;
 
 if(center.x < 300){
  center.x += 10;
  pic.transform = CGAffineTransformMakeScale(.35, 35);
  center.x += 10;
  pic.transform = CGAffineTransformMakeScale(1, 1);
  pic.center = center;
 }
 
}

- (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 {
 [pic release];
    [super dealloc];
}

@end

0 コメント: