I have scroll view with an UIImageView init.I add a button on UIImageView.When i zoom the image then my button is also perform zooming.How can i only zoom the Image not the button.Thanks in advance for you time.
Place the button and any other views you do not want to be affected by the UIScrollView zooming in a view at the same level as the UIScrollView. Here's an example view hierarchy that was set up for just this purpose.
The scroll view and anything inside it will zoom (in this case the Image View).
In the UIView below the scroll view (in this case, an image view with the Default.png image) will not zoom when the scroll view is zoomed.
EDIT:
To have the button remain in the same place relative to the image add it as a subview to your scroll view, add UIScrollViewDelegate to your view controller header. In your view controller implementation: capture the initial UIButton frame in viewWillAppear, and use the scroll view delegate scrollViewDidZoom method to update the button frame so that is stays in place. This will also allow the button to move with the image when panning.
Here's the updated view structure:
The applicable view controller code segments:
#interface MyViewController () {
CGRect initialButtonFrame;
...
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
initialButtonFrame = self.button.frame;
...
}
- (void)scrollViewDidZoom:(UIScrollView *)sv
{
self.button.frame = CGRectMake((initialButtonFrame.origin.x * self.scrollView.zoomScale),
(initialButtonFrame.origin.y * self.scrollView.zoomScale),
initialButtonFrame.size.width,
initialButtonFrame.size.height);
...
}
Here are a couple of images showing the button staying in place relative to its placement in the image. Note the button origin is near the top left of the red area near the center bottom of the fireman's coat when not zoomed (first image) and when zoomed (second image).
Add the button on the imageScrollView superview after the imageScrollView so that the button is not scroll view's subview.
Related
I have a scrollview inside which i have 20 UItextviews. The scrollview is not working. I have set the following in viewdidload.
self.MainScroll.contentSize = CGSizeMake(320, 1800);
Still it doesn't scroll. However, if i give bounce vertically, it just bounces. My scrollview is a child of the main UIview of dimension 320*600. Please guide how to enable the scroll!!
There are two ways you can get the scrolling to work.
Approach 1 (with code):
1) Pin UIScrollView to the sides of its parent view, as mentioned below.
2) Set content size of your scroll view in viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews {
self.MainScroll.contentSize = CGSizeMake(320, 1800);
}
Approach 2 (pure IB, no code required):
1) Setting contentSize is not required if using AutoLayout. You need to pin your UIScrollView to the parent view as mentioned below:
2) Then add another UIView inside UIScrollView to act as a content view and pin it to the UIScrollView and move all controls inside this content view:
3) Pin content view to its parent scroll view as mentioned below:
4) Set your UIViewController's Simulated Metrics to Freeform (this is important):
5) Size your content UIView to your desired height (obviously important too):
Apple article explaining UIScrollView and AutoLayouts:
https://developer.apple.com/library/content/technotes/tn2154/_index.html
Update the content size after some delay as below.
- (void)viewDidLayoutSubviews {
[self performSelector:#selector(updateContentSize)
withObject:nil
afterDelay:0.25];
}
-(void)updateContentSize{
UIView *viewLast = [viewContent viewWithTag:100];
scrollViewAd.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(viewLast.frame));
}
I have a UIScrollView that is 320 by 100 (width and height) and UIPageControl with 4 pages. My App has 4 icons at the top in the scroll view but only displays one of the icons with portions of the other icons on the edges (so the user can know that they can scroll). The user can either tap on the pagecontrol to have it change left or right and it correspondingly changes the icons in the scroll view. On the same point, the user can scroll the scroll view and I want it to center on the icon they are scrolling towards and the page control to change. My issue is that whenever I scroll in the IOS simulator, the icons are offset weirdly and display as such regardless of how much I change the parameters I am moving to. It always skips the second icon.
Here is the code for the ScrollViewDidScroll
`- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float roundedValue = floor(_ScrollView.contentOffset.x / 160);//320
NSLog(#"%f",roundedValue);
self.pageControl.currentPage = roundedValue;
[_dataTable reloadData];
}`
And here is the code for my pageAction
UIPageControl *pageCon = (UIPageControl *)sender;
int pageMoved = pageCon.currentPage;
CGRect movedTo = CGRectMake((pageMoved*190), 0, 320, 100);//160
[_ScrollView scrollRectToVisible:movedTo animated:YES];
All the math works out, im just not sure why its offsetting the images and skipping the second icon.
So I am using WEPopover to display a custom view controller pop up. I have a UIView in which inside it has another UIView called containerView. Inside this containerView, I have a UIButton. This is where I wanted to present my popover from. So here's what I did:
[self.popoverDialog presentPopoverFromRect:sender.frame inView:self.containerView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
The issue is that the arrow and everything is showing from this button, but the popover goes out of self.containerView bounds. How can I make it so that the popover is displayed within the containerView bounds?
EDIT:
A picture is worth a thousand words, so here it is:
The light gray is the containerView I mentioned above. THe popover theoretically should be shown within that light gray bounds not going outside.
For the view that is contained inside the popover, go to it's view controller and set it's property contentSizeForViewInPopover. Here you can set the size so that it fits the bounds of your containerView.
OP wants to position the popover so that it only shows up in his container view. I found this bit of code in the WEPopoverController.m file
- (void)repositionPopoverFromRect:(CGRect)rect
inView:(UIView *)theView
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections {
CGRect displayArea = [self displayAreaForView:theView];
WEPopoverContainerView *containerView = (WEPopoverContainerView *)self.view;
[containerView updatePositionWithAnchorRect:rect
displayArea:displayArea
permittedArrowDirections:arrowDirections];
popoverArrowDirection = containerView.arrowDirection;
containerView.frame = [theView convertRect:containerView.frame toView:backgroundView];
}
You could possibly call this method and it might reposition your popover so that it is now inside your container viw.
I'm trying to draw in a scrollview on top of an image using multi-touch. The circles are actually drawn, but under the image. In my code I currently create a custom ScrollView:
#interface AppScrollView : UIScrollView {
//objects to draw circles
NSMutableDictionary *circlesInProcess;
NSMutableArray *completeCircles;
}
#end
In my AppScrollView.m I override touches commands to store touches and add a
(void)drawRect:(CGRect) rect
method to draw the circles in the scrollview. I also include
[self setNeedsDisplay]
to display the circles. In my app controller, I declare the new custom scrollView object:
IBOutlet AppScrollView *scrollView;
Later I add an image to the scrollView using:
[scrollView addSubview:myImage];
The image is visible, but when I try to draw circles on top of it, they are drawn under the image. Let me know if you have any suggestions?
Suggestion: draw the image onto your scrollview (instead of adding the image view as a subview) and then draw the circles on top of that.
In your code check where did you add you custom scrollview. Is it added after imageView is added or before.
You scrollView should be on top of imageView.
I just created an animation to zoom in or zoom out a UIScrollView, but my problem is that the zoomScale is not updating. Like for example, I zoom out my UIView inside of my UIScrollView using the animation I created, but when I zoom in using expand gesture the UIView will automatically change to its big size or zoom in size. I saw the my zoomScale is not updating and still in 1.0 scale. Then I update my zoomScale after the animation but the problem is the zoom out UIView will automatically go to the top of the screen. Can I update this zoomScale property of UIScrollView?
Thanks.
Did you implement viewForZoomingInScrollView function ?
This function of your scroll view delegate is called when you change the zoomScale. It returns the view that must be rescaled when the scale of your scrollview is changed.
In general you just return the scrollView given in parameters :
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; {
return scrollView.childView;
}
Don't forget to set a delegate to your scrollview : yourScrollView.delegate = self;