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];
}];
Related
In my application , used gallery concept. And this want to Zoom Image with double tap and using 2 finger.
I used below code for this but its not working for me. i enable to achieve this functionality..
Please resolve my problem.
- (void)viewDidLoad
{
[self setupScrollView];
}
-(void) setupScrollView
{
//add the scrollview to the view
image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image_scroll.pagingEnabled = YES;
[image_scroll setAlwaysBounceVertical:NO];
//setup internal views
for (int i = 0; i < [self.PhotoImgArr count]; i++) {
current_page =(int)self.PhotoImgArr[i];
CGFloat xOrigin = i * self.view.frame.size.width;
AsyncImageView *image = [[AsyncImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.imageURL = [NSURL URLWithString:self.PhotoImgArr[i]];
image.contentMode = UIViewContentModeScaleAspectFit;
[image_scroll addSubview:image];
}
//set the scroll view content size
[image_scroll setShowsHorizontalScrollIndicator:NO];
[image_scroll setShowsVerticalScrollIndicator:NO];
[image_scroll setMaximumZoomScale:8.0];
self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
image_scroll.decelerationRate = UIScrollViewDecelerationRateFast;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[image_scroll addGestureRecognizer:doubleTap];
image_scroll.contentSize = CGSizeMake(self.view.frame.size.width *
self.PhotoImgArr.count,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:image_scroll];
[image_scroll setZoomScale:[image_scroll minimumZoomScale]];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if(image_scroll.zoomScale > image_scroll.minimumZoomScale)
[image_scroll setZoomScale:image_scroll.minimumZoomScale animated:YES];
else
[image_scroll setZoomScale:image_scroll.maximumZoomScale animated:YES];
}
You should set userInteractionEnabled of image_scroll
image_scroll.userInteractionEnabled = YES;
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.
i have scroll View with three slides,i want to add images to my scroll view.this is my code after this what do i need to add
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
If you want to add Images horizontally (say 3 images), than you have to add scroll view with the width of 3*yourImageView.frame.size.width and than you can add those Image on (x=0,y=0),
(x = yourImageView.frame.size.width, y=0), and (x = 2*yourImageView.frame.size.width, y=0)
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)
[scroll addSubview:imageView1];
[scroll addSubview:imageView2];
[scroll addSubview:imageView3];
enable horizontal 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));
}
I am using multiple UIScrollViews on-top of one another and Im using a UISegmentedControl to manage which one is active, my issue is, is that once I have set the position of a UIScrollView and gone to another, when I try and return I can only control the last selected one and the UIScrollView stays in the background. Does anyone know how to bring that UIScrollView back to the top?
Below is my code, cheers in advance!
- (void)layerSelected:(id)sender
{
int index = filterControl.selectedSegmentIndex;
switch (index)
{
case 0: if (scroll == nil)
{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.delaysContentTouches = NO;
scroll.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll setContentOffset:currentPos1];
[self.view addSubview:scroll];
[scroll release];
}
else
{
[self.scroll setContentOffset:currentPos1];
scroll.scrollEnabled = YES;
}
break;
if both scroll views are a subview of the main view then you need to call [mainView bringSubviewToFront:scrollView];