2012-07-18 5 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

+0

코드에 메모리 누수가 있습니다. 어레이에 요소를 추가 한 후 oneelement를 릴리스해야합니다. – tikhop

답변

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

안녕하세요! onselement가 필요하지 않았습니다. H. 프로그래밍 방식으로 그림을 만듭니다. – JinDeveloper

+0

및 코드가 작동하지 않습니다 = ( – JinDeveloper

+0

) UIImageView를 초기화하는 코드 줄 하나를 추가하십시오 : self.oneselement.userInteractionEnabled = YES; – tikhop

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

관련 문제