Adding long press gesture to UIImageView inside Scrollview - ios

I've been researching this for a couple of hours now and nothing seems to work. I have a UIScrollView as an IBOutlet and am adding two UIImageViews via code. One is a background image and the 2nd is a photo. Everything works fine here. I am trying to add a long press gesture to the second photo but it doesn't seem to be working. Here is my code. I hope someone can see my problem.
// loop thru images array
UIImage *bgFrame = [UIImage imageNamed:#"photo-frame.png"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgFrame];
bgImageView.frame = CGRectMake(posX, posY, 100, 100);
[self.scroll addSubview:bgImageView];
NSString *photoName = [self.photoArray objectAtIndex:i];
UIImage *photo = [self.utils getClientPhoto:photoName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(6, 9, 89, 71);
imageView.tag = i;
[bgImageView addSubview:imageView];
// add long press for deletion
UILongPressGestureRecognizer *lPressed = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(imageLPressed:)];
lPressed.delegate = self;
lPressed.minimumPressDuration = 0.4;
[imageView addGestureRecognizer:lPressed];

imageView.userInteractionEnabled = YES; not needed, it always work fine. Setting delegate to gestureRecognizer is optional too. First of all try to add imageView to self.scroll subviews not to bgImageView. After that check your scrollView content size, if your image is out of dat size it can be visible but not available. Try to replace imageview with uibutton and check for it's uitouchupinside event.

Enable UserInteraction on bgImageView andimageView.

Related

Bug about UIImageView display gif?

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

Adding a UIImageView (with a two finger pinch) on MKMapView disables scroll on map

I wish to add an image on top of a mapView the purpose of which is to calculate the area contained under that image. Having less or no experience in maps, I am not really sure how to go about it. So, here is what I used.
image = [UIImage imageNamed:#"Fla.png"];
img = [[UIImageView alloc] initWithImage:image];
img.userInteractionEnabled = YES;
img.backgroundColor = [UIColor clearColor];
img.contentMode = UIViewContentModeCenter;
img.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
twoFingerPinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(twoFingerPinch:)];
[img addGestureRecognizer:twoFingerPinch];
[self.mapView addSubview:img];
The image is being displayed fine and the pinch to zoom is working fine as well but the problem is the map stops responding as soon as the imageView is added. If I remove the imageView, the map works fine again.
Also came across this but didnt work for me.
Any help will be much appreciated.
Answering this so it helps those who make as silly a mistake as I did.
Make sure you do not set the frame of the imageView to cover the frame of the screen. In my case, this line killed me -
img.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

How to display image in iphone simulator using objective C

I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.

I don't get why this #selector isn't working

I am creating several UIView's which I add a UITapGestureRecognizer with an action thats suppose to call a method in the viewController, but it doesn't seem to work.
-(void)setupFreeMoneyView {
NSArray *array = #[#"Facebook", #"Twitter", #"SMS", #"Email"];
int index = 0;
for (NSString *str in array) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#Logo", str]]];
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:NSSelectorFromString([NSString stringWithFormat:#"postTo%#", str])]];
[freeView addSubview:imgView];
}
}
freeView is a UIView created in the MainStoryboard.
Ive checked if its the NSSelectorFromString() by using a normal #selector() thats not working but it isn't the cause.
It does work if I change the line [imgView addGestureRecognizer:... to [freeView addGestureRecognizer:...]
I just don't seem to understand why this is happening.
try this
imgView.userInteractionEnabled = YES ;
userInteractionEnabled of UIImageView is set to NO by default.
maybe you need set userInteractionEnabled = YES for the UIImageView

Adding a UIImageView as a subview of UILabel, image not appearing

So, when I do this with a regular old view:
UIView *topBlock = [[UIView alloc] initWithFrame:CGRectMake(0,-frameSize.height, frameSize.width, frameSize.height/2)];
[viewController.view addSubview:topBlock];
topBlock.autoresizesSubviews = NO;
topBlock.clipsToBounds = YES;
UIImage *topImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = viewController.view.frame;
[topBlock addSubview:topImageView];
I get the nice old image where I want it, in the top view. But the middle view is a UILabel, and when I try the same thing:
UILabel *midBar = [[UILabel alloc] initWithFrame:CGRectMake(midBarOrigin.x, midBarOrigin.y, midBarWidth, midBarHeight)];
midBar.text = #"Blokt";
midBar.textColor = [UIColor blackColor];
midBar.textAlignment = NSTextAlignmentRight;
midBar.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[viewController.view addSubview:midBar];
midBar.autoresizesSubviews = NO;
midBar.clipsToBounds = YES;
UIImage *midImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *midImageView = [[UIImageView alloc] initWithImage:midImage];
midImageView.frame = viewController.view.frame;
[midBar addSubview:midImageView];
I don't see any image at all in the UILabel. Any help?
Seems like the issue is related to your frames.
Tough to say without additional info. Can you post viewController.view.frame, frameSize, and midBarOrigin / midBarWidth / midBarHeight?
In the second codeblock, midBar.clipsToBounds == YES, but it looks like the midImageView.frame is likely very different / outside of midBar.frame in which case it wouldn't be visible.
Edit Some screenshots would help but aren't necessary
Edit 2 Note that subviews' origin points are always relative to the coordinate system of their superview, never relative to the coordinate system of any other view in the view heierarchy. This is likely the heart of the issue here. If you do want to convert CGPoints or CGRects from one coordinate system to another there are methods on UIView such as convertRect: and convertPoint: etc.
Interface Builder doesn't even let you add a control inside of a UILabel.
Instead, if you wish to group multiple controls, you can add them both as subviews of a UIView.
In other words, your image view and label can share the same superview, but the image view cannot be a subview of the label.
If they share the same superview, you can position the image view behind the label, and it should appear "through" the label as long as the label's background is clear.
Simple Way to do.....
You can add UIImageView, UILabel as subview of cell.textLabel
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 8, 8)];<br/>statusImage.backgroundColor = [UIColor redColor];<br/>
statusImage.layer.cornerRadius = 4;<br/>
statusImage.clipsToBounds = YES;<br/>
[cell.textLabel addSubview:statusImage];<br/>
UILabel *Lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, cell.textLabel.frame.size.width - 15, cell.textLabel.frame.size.height)];<br/>
Lbl.text = #"xyz";<br/>
[cell.textLabel addSubview:Lbl];<br/>
I just had this problem as well.
I found that ImageView was behind the label.
When I replaced label with UIView, it works properly.

Resources