How to zoom a UIImageView within a UIScrollView? - ios

I have a UIImageView within a UIScrollView and I can vertically scroll, but not horizontal scroll because of how I set the content size. That's exactly how I want.
However, I can't seem to zoom in and out. I set the minimum and maximum zoom scale, but it's not working.
Can someone tell me why?
Thanks.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.scrollView.scrollEnabled = YES;
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.zoomScale = 1.0;
self.scrollView.delegate = self;
[self.scrollView addSubview:tempImageView];
}

You have to implement the following delegate method for zooming:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
and make sure that you add self.imageView to your self.scrollView instead.
It should look like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.delegate = self;
[self.scrollView addSubview:self.imageView];
}

To get zooming to work, you need to provide a delegate method that returns the view you wish to get zoomed:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView
}
You will also need to hold on assign tempImageView to a property so that you can refer to it here. If you don't want to hang on to it in a property, you will need some other way of identifying the view, e.g. scrollView.subviews[0] if it is the only subview.

Try this Try this
[scroll setUserInteractionEnabled:YES];
And your Maximum zoom scale is strange. Try with 2,0 to test.

Related

Programmatically added subview frames don't show up right in scrollview

I'm creating a scrollView programmatically, and all the views in it. However, when I add more then 1 view, the other views layers don't show up right in their respective frames.
For example:
-(void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 150*5);
self.scrollView.backgroundColor = [UIColor lightGrayColor];
self.scrollView.scrollEnabled = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
[self setUpLogInButtons];
}
Then I set the subViews to scrollView up here:
-(void)setUpLogInButtons {
CGFloat subviewWidth = self.scrollView.frame.size.width/2;
CGFloat subviewHeight = 150;
UIView *facebookView = [[UIView alloc] init];
facebookView.frame = CGRectMake(0, 0, subviewWidth, subviewHeight);
[facebookView.layer insertSublayer:[self gradientLayerForViewFrame:facebookView.frame withColors:[self colorArrayWithValues:0x4A5C81 v2:0x495B82 v3:0x495B82 v4:0x475B85 v5:0x465B87 v6:0x455A88 v7:0x445A8A v8:0x435A8B v9:0x425A8D v10:0x41598F v11:0x405990 v12:0x3F5992 v13:0x3E5993 v14:0x3D5895 v15:0x3C5896 v16:0x3B5898 v17:0x3A589A]] atIndex:0];
[facebookView addSubview:[self label:self.facebookLabel forView:facebookView withName:#"Facebook" loggedIn:NO]];
[self.scrollView addSubview:facebookView];
}
When it's one subView it works fantastic, when I add the other one it's not right:
-(void)setUpLogInButtons {
...
UIView *facebookView = [[UIView alloc] init];
...
[self.scrollView addSubview:facebookView];
UIView *twitterView = [[UIView alloc] init];
twitterView.frame = CGRectMake(subviewWidth, 0, subviewWidth, subviewHeight);
[twitterView.layer insertSublayer:[self gradientLayerForViewFrame:twitterView.frame withColors:[self colorArrayWithValues:0x2BA8D5 v2:0x28A8D6 v3:0x25A8D8 v4:0x22A8D9 v5:0x20A9DB v6:0x1DA9DC v7:0x1AA9DE v8:0x18AADF v9:0x15AAE1 v10:0x12AAE3 v11:0x10ABE4 v12:0x0DABE6 v13:0x0AABE7 v14:0x08ACE9 v15:0x05ACEA v16:0x02ACEC v17:0x00ADEE]] atIndex:0];
[twitterView addSubview:[self label:self.twitterLabel forView:twitterView withName:#"Twitter" loggedIn:NO]];
[self.scrollView addSubview:twitterView];
}
This one does not show up, the layers specifically are off. However, here is the kicker, the label shows in the right spot because the labels frame is based on it's parent view frame.
-(UILabel *)label:(UILabel *)viewLabel forView:(UIView *)accountView withName:(NSString *)name loggedIn:(BOOL)loggedinStatus {
UILabel *label = viewLabel;
label.frame = CGRectMake(0, accountView.frame.size.height - 50, accountView.frame.size.width, 50);
label.text = [self setAccountLabelForName:name loggedIn:loggedinStatus];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentLeft;
return label;
}
here is a picture that shows you that the label is based off the parents view frame, and it shows up correctly, but the gradient does not, and it's based off the same frame.
and my gradient method:
-(CAGradientLayer *)gradientLayerForViewFrame:(CGRect)viewFrame withColors:(NSArray *)colors {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = viewFrame;
gradient.colors = colors;
return gradient;
}
For some reason when inserting my gradient layer to the views layer I had to change it:
//INSTEAD OF THIS:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.frame...];
//THIS WORKED FLAWLESSLY:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.bounds...];

UIScrollView to start at bottom view

I am new in IOS Program.
I have UIscrollview with 2 vertical views and would like the page to load automatically on the 2nd/bottom view and to scroll upwards
here is my code for UIScrollView:
// Set up ScrollView
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
CGFloat y = i * self.view.frame.size.height;
UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height)];
[scrollview addSubview:pageView];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *numberOfViews);
scrollview.bounces = NO;
[self.view addSubview:scrollview];
}
Use this in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
CGPoint contentOffset = CGPointMake(0, self.view.frame.size.height);
scrollView.contentOffset = contentOffset;
}
and in viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGPoint contentOffset = CGPointZero;
[scrollView setContentOffset:contentOffset animated:true];
}
I assume you are creating that scrollview in viewDidLoad - just add this code into viewDidAppear and set up proper animation duration:
[scrollView setContentOffset:CGPointMake(0, self.view.frame.size.height)];
[UIView animateWithDuration:5.0 animations:^{
[scrollView setContentOffset:CGPointZero];
}];

Why am I unable to zoom my UIImageView embedded in a UIScrollView?

I literally have the most basic of code and I'm so frazzled as to why it won't zoom:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.bounds.size;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 10.0;
[self.view addSubview:self.scrollView];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"community1.jpeg"]];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:self.imageView];
}
In the simulator I try to zoom and nothing at all happens. It's a rather large image if that's relevant.
Did you implement -viewForZoomingInScrollView: in your scroll view's delegate? If not, you need to do that, returning the view you want to actually zoom when you pinch.

UIScrollView programmatically Scrolling

I have a UITextView inside a UIScrollView that needs to automatically scroll once the user starts editing it. This is because the keyboard will cover the textview.
Here is the code -
In viewDidLoad:
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
and here's the delegate method implementation -
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[scrollView setContentOffset:point animated:YES];
}
However, nothing happens.
your are doing fine but use feedBackformView instead of scrollView,while setting content offset in textViewDidBeginEditing:method, just have a look on below code
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];
in textview DelegateMethod,
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[feedBackformView setContentOffset:point animated:YES];
}
hope it will hepls you...
You can use scrollView scrollRectToVisible:animated or the setContentOffset:animated methods described here:
UIScrollView Class Reference
Keep in mind that UITextView comes with its own scroll view. so you may not need to nest it inside another scrollview. But you may need to if your app is that interesting.
Is your point of scrolling already at a visible point?
-(void)textViewDidBeginEditing:(UITextView *)textView{
// try this instead
CGPoint point = CGPointMake(textView.frame.origin.x,
textView.frame.origin.y + textView.frame.size.height);
[scrollView setContentOffset:point animated:YES];
// What does this produce?
NSLog(#"%# %#", NSStringFromCGPoint(scrollView.contentOffset),
NSStringFromCGRect(textView.frame));
}

Large UIImage not scaling down into Scroll View's frame

i can't seem to use UIViewContentModeScaleAspectFit property to scale my imageView within my scrollview to fit the scrollview size.
- (void)loadView {
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.moilum.com/images/atomicongallery/setmenu/setmenu1.jpg"]]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:applicationFrame];
[scrollView addSubview:imageView];
scrollView.contentSize = image.size;
scrollView.minimumZoomScale = 0.3;
scrollView.maximumZoomScale = 3.0;
scrollView.clipsToBounds = NO;
scrollView.delegate = self;
self.view = scrollView;
}
i have tried imageView.contentMode = UIViewContentModeScaleAspectFill as well. scrollView.contentmode = UIViewContentModeScaleAspectFill as well. All dont seem to work :(
any way out!?
Cheers!
The UIScrollView will not adjust its zoomScale if you don't implement the - (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView method on the UIScrollViewDelegate.
Something like this
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView
{
NSArray *subViews = [sView subviews];
if([subViews count] > 0){
return [subViews objectAtIndex:0];
}
return nil;
}
(But apparently not setting a delegate at all works too.)
Have you tried,
-(void)loadView
{
CGRect frame = CGRectMake(0, 0, scrollView.frame.size.width, scrolllView.frame.size.height);
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubView:imageView];
scrollView.contentSize = CGSizeMake( imageView.frame.size.width, imageView.frame.size.height);
}
I write this code from my iPad i think this should work,
Let me know if you have any problem.
Good luck.

Resources