Set image to imageView by ID programmatically in iOS - ios

I'm developing an iOS app and i have a for loop to create images.
When an image receives the click, i would like to change the background of the image, only to that image. how can I do this?
This is my code:
for (int i =0; i<dim; i++) {
likeImageImageview = [[UIImageView alloc] initWithFrame:CGRectMake(paddWidth*20,7.5,39.6,39.6)];
img0 = [UIImage imageNamed:#"like"];
[likeImageImageview setImage:img0];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapImagesLike)];
singleTap.numberOfTapsRequired = 1;
likeImageImageview.userInteractionEnabled = YES; //l'utente può cliccare
[likeImageImageview setMultipleTouchEnabled:NO]; //non è consentito il multitouch
[likeImageImageview addGestureRecognizer:singleTap];
[row_like addSubview:likeImageImageview];
}
And this is the method:
-(void)tapImagesLike{
likeImageImageview.image = [UIImage imageNamed:#"mylike"];
}
With this code, only the last imageview changes its background. How can I use "i" like an ID?

for loop
UIImageView *imageView = …
imageView.tag = i;
So you need to get the view that was tapped
- (void)tapImagesLike:(UITapGestureRecognizer*)recognizer
{
UIImageView *imageView = (UIImageView*)recognizer.view;
int i = imageView.tag;
// Then you can do the change you need to do here
}
This is the method that should be called when the gesture recognizer tap occurs.

It only display the last image because your are creating a new instance in a for loop.
Try this
NSMutableArray *tempImageArray=[[NSMutableArray alloc]init];
for (int i =0; i<10; i++) {
UIImageView *likeImageImageview = [[UIImageView alloc] initWithFrame:CGRectMake(20,7.5,39.6,39.6)];
UIImage *img0 = [UIImage imageNamed:#"like"];
[likeImageImageview setImage:img0];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapImagesLike:)];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
[row_like addSubview:likeImageImageview];
[tempImageArray addObject:likeImageImageview];
}
self.imageArray=tempImageArray;
[tempImageArray release];
And
- (void)tapImagesLike:(UITapGestureRecognizer*)recognizer{
UIImageView *imageView = (UIImageView *)[self.imageArray objectAtIndex:0];
imageView.image=[UIImage imageNamed:#"0.png"];
}

Related

Prevent simultaneous click on Imageviews

I have 3 image views and I would like to prevent simultaneous taps on image views. how can I do that? Can anyone help me??
for (int i=0; i <= [_images1 count]-1; i++){
CGFloat xOrigin = i * self.view.frame.size.width/3;
wordsImage = [[UIImageView alloc] init];
[wordsImage setFrame:CGRectMake(xOrigin+20, self.view.frame.size.height/3,self.view.frame.size.width/3.5 , self.view.frame.size.height/5)];
[wordsImage setImage:[UIImage imageNamed: [_images1 objectAtIndex:i]]];
[self.view addSubview:wordsImage];
[wordsImage setTag:i];
wordsImage.userInteractionEnabled = YES;
tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:index_image:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[wordsImage addGestureRecognizer:tapGesture1];
}
Use this method to restrict the sequence of click your same imageview. I hope this will help you.
int previousTag,curentTag,flag;
-(void)tapGesture:(id)sender{
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
UIImageView *imageView = (UIImageView *)recognizer.view;
if(flag == 0){
previousTag = imageView.tag;
curentTag = 520; // unequal value you will enter here
flag = 1;
}
else {
curentTag = imageView.tag;
}
if(previousTag != curentTag)
{
[imageView setImage:[UIImage imageNamed:#"anyImage.png"]];
previousTag = curentTag;
}
}
If you want to prevents simultaneous click on Imageviews, you can set exclusiveTouch by YES.
/* exclusiveTouch
A Boolean value that indicates whether the receiver handles touch events exclusively.
Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.
*/
for (int i=0; i <= [_images1 count]-1; i++){
CGFloat xOrigin = i * self.view.frame.size.width/3;
wordsImage = [[UIImageView alloc] init];
[wordsImage setFrame:CGRectMake(xOrigin+20, self.view.frame.size.height/3,self.view.frame.size.width/3.5 , self.view.frame.size.height/5)];
[wordsImage setImage:[UIImage imageNamed: [_images1 objectAtIndex:i]]];
[self.view addSubview:wordsImage];
[wordsImage setTag:i];
wordsImage.userInteractionEnabled = YES;
wordsImage.exclusiveTouch = YES;//Set this property
tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:index_image:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[wordsImage addGestureRecognizer:tapGesture1];
}
I hope this would be helpful
W hen you tap the image view set one Bool variable to true and manage that variable.

How can I access a uiview cusom property like tag on tap gesture in Objective-C?

Actually i want to do animation in objective.I have added list of imageview(subclassed) to view and stored all imageView in Array as following
CustomImageView *imageView = [[CustomImageView alloc]init];
imageView.tag = index;
imageView._x = (i%4)*150;
imageView._y = int(i/4)*150;
imageView.vx = 0;
imageView.vy = 0;
imageView.dragging = false;
imageView.frame = CGRectMake(imageView._x, imageView._y, imageView._width, imageView._height);
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imageView addGestureRecognizer:tapGesture1];
[self.view addSubview:imageView];
[imageViewsArray addObject:imageView];
can we access to change values of specific imageView property(subclass property) by following code
UIImageView * imageView = [imageViewsArray objectAtIndex:tag];
imageView.dragging = true;
imageView.vx = 0;
imageView.vy = 0;
Hmmm... um yes, it would work I think.
Not quite sure what you're trying to do here (especially given your x/y being based on the index), but in general instead of creating a CustomImageView for each image (which you would then have to insert into the view hierarchy), perhaps load the images (i.e., a UIImage) into your array instead and THEN swap them into your CustomImageView.

Enlarge image when imageview inside a scrollview is tapped

I have an app that implements Horizontal gallery. I am able to display the image thumbnails. Now what I need to do is to enlarge the image when it is tapped. I am using EMNotificationPopup to display the enlarged image. Here's what I have so far:
This snippet is in my viewdidload:
UITapGestureRecognizer *singleTap;
UIImageView * imageview;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
[singleTap setNumberOfTouchesRequired:1];
[singleTap setNumberOfTapsRequired:1];
singleTap.delegate = self;
[imageview setUserInteractionEnabled:YES];
[imageview addGestureRecognizer:singleTap];
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
[self coreManagementWithPosition:EMNotificationPopupPositionCenter andType:EMNotificationPopupBig];
}
- (void) coreManagementWithPosition: (EMNotificationPopupPosition) position andType:(EMNotificationPopupType) notificationPopupType {
if (_notificationPopup.isVisible) {
[_notificationPopup dismissWithAnimation:YES];
_notificationPopup = NULL;
} else {
_notificationPopup = [[EMNotificationPopup alloc] initWithType:notificationPopupType enterDirection:EMNotificationPopupToDown exitDirection:EMNotificationPopupToLeft popupPosition:position];
_notificationPopup.delegate = self;
_notificationPopup.title = #"Sorry for this Alert message :)";
_notificationPopup.subtitle = #"Awesome message :)";
_notificationPopup.image = [UIImage imageNamed:#"alert_image"];
if (notificationPopupType == EMNotificationPopupBigButton)
_notificationPopup.actionTitle = #"OK";
[_notificationPopup show];
}
}
My issue here is handleImageTap is not being initialised/called. How can enable the user interaction for imageview.

add UISwipeGestureRecognizer to UIimageView that made by array

Code:
Header file:
#interface game : UIViewController
{
UIImageView *anh[8][8];
}
-(void)SwipeToMove:(id)sender;
#end
Implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeToMove:)];
[move setDirection:(UISwipeGestureRecognizerDirectionUp)];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
anh[i][j]=[[UIImageView alloc] initWithFrame:CGRectMake(0+40*i,200 + 40*j,40,40)];
anh[i][j].userInteractionEnabled = YES;
[self.view addSubview:anh[i][j]];
anh[i][j].image = [UIImage imageNamed:#"Earth.png"];
[anh[i][j] addGestureRecognizer:move];
}
}
}
-(void)SwipeToMove:(id)sender{
NSLog(#"ok");
}
The UIImageView works as expected, but UISwipeGestureRecognizer is not working. Then, I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(100,50,40,40)];
label1.image = [UIImage imageNamed:#"Ceres.png"];
[self.view addSubview:image1];
image1.userInteractionEnabled = YES;
image1.image = [UIImage imageNamed:#"Earth.png"];
UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeToMove:)];
[move setDirection:(UISwipeGestureRecognizerDirectionUp)];
[image1 addGestureRecognizer:move];
-(void)SwipeToMove:(id)sender{
NSLog(#"OK");
}
It's working, but I need an array of UIImageViews to make my app. Please help me.
Your code is only creating one gesture recognizer, but you try to add it many image views. You need to create the gesture recognizer inside the loop so you create a new one for each image view.
(void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
UISwipeGestureRecognizer *move = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeToMove:)];
[move setDirection:(UISwipeGestureRecognizerDirectionUp)];
anh[i][j]=[[UIImageView alloc] initWithFrame:CGRectMake(0+40*i,200 + 40*j,40,40)];
anh[i][j].userInteractionEnabled = YES;
[self.view addSubview:anh[i][j]];
anh[i][j].image = [UIImage imageNamed:#"Earth.png"];
[anh[i][j] addGestureRecognizer:move];
}
}
}
You are adding the same gesture recognizer all the time to the different images, so it will take just the last one for my understanding, because you have just created one. Create an aux recognizer like an aux var inside the loop and give a tag to every UIImageView so you can access to the tag and distinguish every swipe in every image, when it has happened in your app. It should work. But basically :
for (....){
// create aux gesture recognizer and assign it to the UIImageView
}
Hope it helps.

selector function not called when adding list of image views with tap gesture recognizer

I have trouble with creating list of ImageViews with tap gesture. When I create gesture, selector function is not called. When I create only one imageView function is called. Any idea why? It is all subview of a large scroll view.
This is my code:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[myScroll addSubview:clickForDetail];
tag++;
}
}
and selector function:
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
}
Is it possible somehow to get tag of a ImageView that is clicked?
You need to put different tap gesture for each view object
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[epgScroll addSubview:clickForDetail];
tag++;
}
}
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
UIImageView *selectedTextView = (UIImageView *)recognizer.view;
}

Resources