2011-10-31 3 views
0

PhotoScroller 프로젝트를 사용하고 있으며 프로그래밍 방식으로 imageViews 위에 버튼을 만들고 싶습니다. ImageView는 ScrollerView의 하위 뷰입니다. 내 코드 스 니펫은 아래에 표시됩니다.고유성을 유지하면서 동적 이미지 뷰를 통해 동적 버튼을 만드는 방법

imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image 

//imageView.tag = i; // tag our images for later use when we place them in serial form 
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact 
imageView.userInteractionEnabled = YES; //to enable touch interaction with image 

[self addSubview:imageView]; 

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button 

btn.frame = CGRectMake(215, 550, 100, 100); 
[btn addTarget:self action:@selector(onClickHotSpotButton)   forControlEvents:UIControlEventTouchUpInside]; //it wasnt working 

[self addSubview:btn]; //Buttons are clickable but shows button on all images** 

//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images 

나는 지금 두 가지 선택 사항을 가지고 있습니다. 내가 내 버튼을 ImageView의 하위 뷰 또는 ImageView의 부모 인 ScrollView로 만들지 여부. Scrollview의 버튼 하위 뷰를 [self addSubView : btn]처럼 만들면; 그것은 오른쪽 위치에 표시 할 수 있으며 클릭 가능하지만 문제는 부모보기의 하위보기, 즉 scrollview로 만들기 때문에 대기열에있는 모든 이미지에 문제가 있다는 것입니다. 그렇지 않으면 모든 이미지에 대해 고유 한 ImageView 및 하위 이미지 뷰의 하위 뷰를 만들지 만 모든 이미지와 클릭 할 수없는 이미지에 여전히 표시됩니다./

클릭 할 수있는 방법과 클릭 연결 방법을 안내 할 수 있습니까? 동적 버튼과 고유 한 이미지를 사용하여 대기열에있는 각 이미지의 다양한 위치에 다른 버튼이 있습니다.

미리 감사드립니다.

감사합니다,

wahib

답변

2

당신은있는 UIImageView하고있는 UIButton

UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0,0,1000,1000)] autorelease]; 
UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; 

[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact 
imageView.userInteractionEnabled = YES; //to enable touch interaction with image 

[imageContainer addSubview:imageView]; 

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button 

btn.frame = CGRectMake(215, 550, 100, 100); 
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working 

[imageContainer addSubview:btn]; 
[self addSubview:imageContainer]; 

메모리 누수에주의를 모두 포함하기위한 여분의 UIView를 만들어야합니다. 코드에서 사용되지 않은 항목이 많이 있습니다.

관련 문제