2012-07-18 35 views
1

我寫了下面的代碼,它不起作用。圖片顯示,但我不能移動它以編程方式創建的移動uiimageview

UIImageView *oneselement = [[UIImageView alloc] initWithFrame:self.view.frame]; 
oneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]]; 
oneselement.frame = CGRectMake(0, 0, 122, 122); 
[self.view addSubview:oneselement]; 


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view]==oneselement) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     oneselement.center=location; 
    }} 

我加了圖片查看,並寫了下面的代碼

.H

@interface ViewController : UIViewController 
{ 
    IBOutlet UIImageView *oneEl; 
} 
@property (nonatomic,retain) IBOutlet UIImageView *oneEl; 

.M

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [[event allTouches] anyObject]; 
    if([touch view]==oneEl) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     oneEl.center=location; 
    } 
} 

它作品! 我該如何編程以便通過uiimageview創建程序可以移動?

的NSMutableArray

self.elements=[myElements getElements]; 

imagesElements = [[NSMutableArray alloc]init]; 
for(ElemetsList *item in self.elements) 
{ 
     UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]]; 
     oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height); 
     oneelement.userInteractionEnabled=YES; 
     [imagesElements addObject:oneelement]; 
} 

for(UIImageView *img in imagesElements) 
    [self.view addSubview:img]; 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 

    for(UIImageView *img in imagesElements) 
    { 
     if([self view] == img) 
     { 
      CGPoint location = [touch locationInView:self.view]; 
      img.center=location; 
     } 
    } 
} 
+0

想要移動圖片或旋轉圖片? – Hiren 2012-07-18 12:15:50

+0

你的代碼中有內存泄漏,你應該在它被添加到數組之後釋放oneelement。 – tikhop 2012-07-18 19:21:02

回答

3

初始化您的UIImageView如下面的代碼,然後它會像你的第二個例子:

ViewController.h

@interface ViewController : UIViewController 
{ 
    UIImageView *oneselement; 
} 
@property (nonatomic,retain) UIImageView *oneselement; 

ViewController.m

//initialize your UIImageView 
UIImageView *newOneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]]; 
self.oneselement = newOneselement; 
self.oneselement.userInteractionEnabled = YES; 
self.oneselement.frame = CGRectMake(0, 0, 122, 122); 
[self.view addSubview:self.oneselement]; 
[newOneselement release]; 

//Handle touchesMoved 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    if([touch view]==self.oneselement) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     self.oneselement.center=location; 
    } 
} 
+0

你好!這是必要的,而不是。 H.編程創建一張圖片。 – JinDeveloper 2012-07-18 14:50:25

+0

和代碼不起作用=( – JinDeveloper 2012-07-18 15:05:45

+0

添加一個代碼行,你初始化UIImageView:self.oneselement.userInteractionEnabled = YES; – tikhop 2012-07-18 15:24:36

0

請參閱this鏈接。
您也可以嘗試:

爲了移動圖像,您應該將代碼中的動畫塊移動:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5]; 
tapImage.center = CGPointMake(156, 110); 
[UIView commitAnimations]; 

您還可以創建一個方法的動畫完成後執行與UIView setAnimationDidStopSelector:方法。

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animateImages)]; 
tapImage.center = CGPointMake(156, 110); 
[UIView commitAnimations]; 

//other stuff 

-(void)animateImages{ 
    [tapImage startAnimating]; 
} 
+0

你好!需要一點點用你的手指移動圖像。 – JinDeveloper 2012-07-18 14:52:28