I want to scroll my UIScrollView (horizontally). But when the button is pressed it goes the left most side of the scroll view. I want to scroll it 3,4 pixels and when I press again it scrolls another 3,4 pixels
- (IBAction)leftScroll:(id)sender
{
CGPoint pt;
pt.x = 1;
pt.y = 0;
UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:5];
[sv setContentOffset:pt animated:YES];
}
Thanks in advance for your answer
Try it and set a new position manually.
Objective-c
float width = CGRectGetWidth(scrollView.frame);
float height = CGRectGetHeight(scrollView.frame);
float newPosition = scrollView.contentOffset.x+width;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[scrollView scrollRectToVisible:toVisible animated:YES];
Swift 4
let scrollView = UIScrollView()
let width: CGFloat = scrollView.frame.size.width
let height: CGFloat = scrollView.frame.size.height
let newPosition: CGFloat = scrollView.contentOffset.x + width
let toVisible: CGRect = CGRect(x: newPosition, y: 0, width: width, height: height)
scrollView.scrollRectToVisible(toVisible, animated: true)
You can use [scrollView setContentOffset:CGPointMake(x, y) animated:YES]; method. If your scrollView scrolls horizontally then you need to set x value accordingly, else y value.
You can scroll to some point in a scroll view with the following in Obj-C
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];
or Swift
scrollView.contentOffset = CGPoint(x: x, y: y)
To do slideshows with UIScrollView, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated: when the timer fires.
UIButton *locatorbutton_frame=(UIButton *)[scrollviewoutlet viewWithTag:numberglobal];
float width = scrollviewoutlet.frame.size.width;
float height = scrollviewoutlet.frame.size.height;
float newPosition = scrollviewoutlet.contentOffset.x-locatorbutton_frame.frame.size.width;//+width;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[scrollviewoutlet scrollRectToVisible:toVisible animated:YES];
I found it working fine locatorbutton_frame is the button that i added to the scrollview.
What about:
Objective-C
CGRect visible = [targetView.superview() convertRect:targetView.frame toView:scrollView];
[scrollView scrollRectToVisible:visible animated:YES];
Swift
let visible: CGRect = targetView.superview().convertRect(targetView.frame, toView: scrollView)
scrollView.scrollRectToVisible(visible, animated: true)
I tested the Objective-C code, and it works in every screen orientation.
Related
I already checked all the answer related to this. My code only works if I disable or enabling scrolling from storyboard not from my code.
It always shows the content size height and tableview height as same. I am not using Autolayout.
[tableView reloadData];
tableView.frame.size.height = tableView.contentSize.height;
Try this
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self adjustHeightOfTableview];
}
write adjustHeightOfTableview this method as
- (void)adjustHeightOfTableview
{
CGFloat height = self.tableView.contentSize.height;
CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
// if the height of the content is greater than the maxHeight of
// total space on the screen, limit the height to the size of the
// superview.
if (height > maxHeight)
height = maxHeight;
// now set the frame accordingly
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = self.tableView.frame;
frame.size.height = height;
self.tableView.frame = frame;
// if you have other controls that should be resized/moved to accommodate
// the resized tableview, do that here, too
}];
}
Hope it helps.
I have made UIScrollView with three UIScrollViews inside and UImageView inside those three scollviews (Like in this answer: https://stackoverflow.com/a/25768875/2911156). Almost everything works, but zooming in landscape mode is broken. When user zoom in image, after zooming is finished, image is moved to the right edge and cut. How to center image after zoom, or what is wrong here?
UI Structure:
Main Scroll view Constraints:
Image View constraint (all three had same pattern)
I'm not doing much in code (ScrollViews and ImageViews are based on autolayout), just done this:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _photoImage;
}
and this:
- (void) setMaxMinZoomScaleForBonds {
self.contentInset = UIEdgeInsetsZero; //need to reset it before load image, because previous insets can be wrong
self.minimumZoomScale = 1.0;
self.zoomScale = 1.0;
self.maximumZoomScale = 2*[UIScreen mainScreen].scale;
}
One more thing done, but it does not work:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
//center image when zooming.
CGFloat x = 0;
CGFloat y = 0;
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHight = [[UIScreen mainScreen] bounds].size.height;
CGFloat viewWidth = view.frame.size.width;
CGFloat viewHight = view.frame.size.height;
if(viewWidth < screenWidth)
x = screenWidth/ 2;
if(viewHight < screenHight)
y = screenHight / 2 ;
if(scale<=1.1)
self.contentInset = UIEdgeInsetsZero;
else
self.contentInset = UIEdgeInsetsMake(y, x, y, x);
}
That is all related to zoom action.
I'd like to create an animation that when I tap on a GMSmarker, a uiview will appear with growing effect from the left or right corner of the given control/ image and include the marker in it's corner (image shows final result)
I know how to create the XIB and the related UIView, as well as adding them to the ViewController, how can I do the animation part?
You know the start frame of the view...
CGRect startFrame = CGRectMake(pin.x, pin.y, 0, 0);
You know the end frame of the view...
CGFloat height = 100;
CGFloat width = 100;
CGRect endFrame = CGRectMake(pin.x, pin.y - height, width, height);
So you can animate it...
theView.frame = startFrame;
[UIView animateWithDuration:1.0 animations:^(){
theView.frame = endFrame;
}];
This will animate out from the bottom left corner.
I am newbie so my question can be really obvious, but I didn't find any solution so far.
I did in IB UIScrollView and connected it with File's Owner.
I would like to set now the frame size of my UIScrollView (named ScrollView).
const CGFloat BoardWidth = 320;
const CGFloat BoardHeight = 400;
//I tried this way but 'Expression is assignable' - said Xcode
ScrollView.frame.size.width = BoardWidth;
ScrollView.frame.size.height = BoardWidth;
So how can I the easiest set own sizes of ScrollView frame?
CGRect scrollFrame = CGRrectMake(x, y, w, h);
ScrollView.frame = scrollFrame;
Or if you need only to change the width and height;
CGRect scrollFrame;
scrollFrame.origin = ScrollView.frame.origin;
scrollFrame.size = CGSizeMake(w, h);
ScrollView.frame = scrollFrame;
Edit keeping the center unchanged:
CGRect scrollFrame;
CGFloat newX = scrollView.frame.origin.x + (scrollView.frame.size.width - w) / 2;
CGFloat newY = scrollView.frame.origin.y + (scrollView.frame.size.height - y) / 2;
scrollFrame.origin = CGPointMake(newX, newY);
scrollFrame.size = CGSizeMake(w, h);
scrollView.frame = scrollFrame;
you need to do this, for example.
ScrollView.frame = CGRectMake(0, 0, BoardWidth, BoardHeight);
Once done you need also to set contentSize property. It's the area that 'extends' behind the frame of your UIScrollView.
If you want to scroll only horizontally:
ScrollView.contentSize = CGSizeMake(w * BoardWidth, BoardHeight);
If you want to scroll only vertically:
ScrollView.contentSize = CGSizeMake(BoardWidth, h * BoardHeight);
If you want to scroll both horizontally and vertically:
ScrollView.contentSize = CGSizeMake(w * BoardWidth, h * BoardHeight);
where w and h are generic values to increment your width and/or your height.
If you want have an horizontal (for example) discrete scroll (e.g. you have some pages with the same dimension of your screen).
ScrollView.pagingEnabled = YES;
ScrollView.contentSize = CGSizeMake(p * BoardWidth, BoardHeight);
where p is the number of pages you want to display.
Hope it helps.
You can do it as below,
scrollView.frame = CGRectMake(xOrigin,yOrigin,width,height);
If you are looking for content size of scrollview then it as below,
scrollView.contentSize = CGSizeMake(width,height);
CGRect Frame = ScrollView.frame
Frame.frame.size.width = BoardWidth;
Frame.frame.size.width = BoardWidth;
ScrollView.frame = Frame;
Since frame is a structure..you can't set the frame of an object directly.. You will have to make a Cgrect and then manipulate it.
using contentSize property
ScrollView.contentSize = CGSizeMake(320, 520);
it needs to be bigger than the frame so it actually scrolls.
Most of the sample code on the net assumes your UIScrollView will be full-screen.
I'm trying to create a UIScrollView with just a UIImageView inside it, and just want to be able to pinch zoom as would normally happen. My UIScrollView only fills a part of the screen, so I can't just use autosizing to keep it the right size, as the sample applications seem to do.
The closest I've got is to add this to the UIScrollViewDelegate:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGPoint cOffset = imageScrollView.contentOffset;
float cScale = imageScrollView.contentScaleFactor;
CGRect imageFrame = ptImage.frame;
float scale = scrollView.zoomScale;
imageFrame.size.width *= scale;
imageFrame.size.height *= scale;
ptImage.frame = imageFrame;
scrollView.zoomScale = 1.0;
scrollView.frame = CGRectMake(0.0, 0.0, scrollContainerView.frame.size.width, scrollContainerView.frame.size.height);
scrollView.bounds = CGRectMake(cOffset.x * cScale * scale , cOffset.y * cScale * scale, scrollView.bounds.size.width, scrollView.bounds.size.height);
scrollView.contentSize = imageFrame.size;
}
The only problem here is that the bounds offset (scrollView.bounds = ...) is incorrect.
Surely it's far simpler than I've done here?
Any help?
Try returning your UIImageView in :
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
And remove the code you put in the didZoom method and see if that works.