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.
Related
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.
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;
}
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"];
}
I want users to be able to use pinch to zoom in my app which uses Storyboard and programmatically generated buttons and labels to produce a View within a Viewcontroller. In the View's Attribute inspector I have checked both User Interaction Enabled and Multiple Touch. But no pinch to zoom seems to work.
The code which creates the important part of the View is as follows.
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger xPipsOrigin = 0;
NSInteger xPipsStep = 22.0;
NSString* cards = #"AKQJT98765432";
NSInteger yPipsOrigin = 100;
if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
xPipsOrigin = 0;
xPipsStep = 22.0;
xPipsStep = 34.0;
}
else{
xPipsOrigin = 100;
xPipsStep = 40.0;
}
NSInteger xPipsCurrent = xPipsOrigin;
self.countCards = 0;
self.cardList = [NSMutableArray arrayWithCapacity:0];
yPipsOrigin = 100;
xPipsCurrent = xPipsOrigin;
for( int x=0;x<[cards length]; x++ ){
[cards substringWithRange:NSMakeRange(x,1)];
UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
xPipsCurrent += xPipsStep;
[b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
[b setTitle:#" " forState:UIControlStateDisabled];
[b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
[b setEnabled:YES];
[b setUserInteractionEnabled:YES];
[self.view addSubview:b];
[b addTarget:self action:#selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
xPipsCurrent = xPipsOrigin + xPipsStep/2;
for( int x=0;x<[cards length]-1; x++ ){
xPipsCurrent += xPipsStep;
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 20, 20)];
lab.text = #"\u2660";
lab.backgroundColor = [UIColor clearColor];
lab.center = CGPointMake(xPipsCurrent+10, yPipsOrigin+10);
[self.view addSubview:lab];
}
}
How can I enable Pinch to Zoom in this app?
If you want to enable pinch to zoom on a UIView you have to wrap it into a UIScrollView first
Whilst you can use User Interaction Enabled and try do it that way, it'll be easier to add a UIGestureRecogniser to your UIView. That way it handles most of the setup code for you, and when it detects a pinch triggers a method.
Basic example in Objective-C:
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
- (void)handlePinchGesture:(id)sender
{
NSLog(#"Recieved pinch");
UIPinchGestureRecognizer *senderGR = (UIPinchGestureRecognizer *)sender;
NSLog(#"Scale is: %f", senderGR.scale);
}
And in Swift:
let pinchGestureRecognizer = UIPinchGestureRecognizer.init(target: self, action: #selector(ViewController.handlePinchGesture(_:)))
self.view.addGestureRecognizer(pinchGestureRecognizer)
func handlePinchGesture(sender:AnyObject) {
print("Received pinch")
let senderGR = sender as! UIPinchGestureRecognizer
print("Scale is \(senderGR.scale)")
}
I'm hoping somebody can help me out here, I'm not sure what else to do. I have a UIScrollView that loads a set of 44 images, and then allows the user to page through them. My code was working, then suddenly it stopped. I have no idea what I have changed so any help would be much appreciated:
The previous view is a UITableView, it passes a string along with the name of the cell that was selected. Here is the ViewDidLoad Method from the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Answer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(answerPressed:)];
self.navigationItem.rightBarButtonItem = rightButton;
if([questionSetName isEqualToString:#"Limitations"]){
[self limitationsView];
}
}
Here is the limitationsView:
-(void)limitationsView{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Images/Limitations"];
NSMutableArray *limitationImages = [[NSMutableArray alloc] init];
for (int x = 0; x<[unsortedArray count]; x++) {
NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];
[limitationImages insertObject:fileName atIndex:x];
}
NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
context:(__bridge void *)([NSLocale currentLocale])];
for(int i = 0; i< [sortedArray count]; i++){
CGFloat xOrigin = i * self.view.bounds.size.width;
UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;
[limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];
limitationImageView.contentMode = UIViewContentModeScaleAspectFit;
[scroll addSubview:limitationImageView];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);
[self.view addSubview:scroll];
}
When I get to the end of the LimitationsView method the sortedArray contains all 44 images, and the proper names, if I try and set the background color of the scrollview I can successfully do that. but my images are not loading and I am at a loss as to why?
I would suggest using
NSLog(#"image:%#",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]);
to see if the image is null at that point. If that's the case, then there is a problem with the strings you are storing in that array i.e., they are not identical to the actual names of your images