UIScrollView doesn't scroll offset content - objective-c

I have an UIScrollView with this parameters:
[scrollView setPagingEnabled:YES];
[scrollView setContentSize:CGSizeMake(1024 * 2, 768)];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
[scrollView setScrollEnabled:NO];
[scrollView setDelegate:self];
For changing page:
CGRect frame = scrollViewPages.frame;
frame.origin.x = 1024;
[scrollViewPages scrollRectToVisible:frame animated:YES];
So this scrollview has a double area for display content. In the first rectangle the touch works, but when I change to the other segment, which has others components with different sizes, touch events don't work. I've read that UIScrollView doesn't apply touch events to hidden sections. I would like to know how update the view for getting touch interactions.
thanks

UPDATE: I was adding the UIViews into another UIVIew inside UIScrollView. Now it works

Related

ScrollRectToVisible function

I have an UIScrollview and which contains UIView as a subview like,
here i can able to drag the UIView inside the ScrollView, when the uiview goes to at END RIGHT or END LEFT the uiview disappears inside scrollview, after that i has to scroll the scrollview then i can able to see the uiview, here i need to SCROLL THE SCROLLVIEW when uiview comes at the rightside or left side, for that i used this code but not working.
[myscrollview scrollRectToVisible:myview.frame animated:YES];
Here I have used on UIButton (as it is already used in one of my application I dont need to write whole code again)
[cropRectangleButton addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
The cropRectangleButton is one UIButton and the imageMoved:withEvent: method is as below
- (IBAction) imageMoved:(id)sender withEvent:(UIEvent *)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
CGPoint prev = lastTouchDownPoint;
lastTouchDownPoint = point;
CGFloat diffX = point.x - prev.x;
CGFloat diffY = point.y - prev.y;
UIControl *button = sender;
CGRect newFrame = button.frame;
newFrame.origin.x += diffX;
newFrame.origin.y += diffY;
scrollView.contentSize = CGSizeMake(2*scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView scrollRectToVisible:newFrame animated:YES];
button.frame = newFrame;
}
Here I change my contentSize of scrollView as it is very narrow to test that the scrollView is scrolling or not you don't need that line of code.
And when I drag the button left the scrollView is also automatically scrolled to the button frame to show the whole button try to implement this using UIView and if not possible just put one transparant UIButton on your view fully covered the view and you it to do the drag :)

How can I use pinch zoom(UIPinchGestureRecognizer) to change width of a UIView

I can get the UIPinchGestureRecognizer handler to work with scaling an object but I don't want to scale I want to change the size. For example I have a UIView and I've attacked a UIPinchGestureRecognizer gesture to it and if the user pinches I want to change the width of the UIView to match the pinch. I don't want to scale it so the UIView is larger(zooming)
If you have the UIPinchGestureRecognizer call a method pinch, you can do:
- (void) pinch:(UIPinchGestureRecognizer *)pinch
{
CGRect frame = [self.view frame];
frame.size.width = frame.size.width * pinch.scale;
[self.view setFrame:frame];
}

UIScrollView remove subview animated

What I have:
Basically I have collection of UIViews presented in horizontal scrollView. UIScrollView have paging enabled and each item is not full screen so I can see part of previous and next view based on this.
Each view have delete button on itself.
What I need:
I need to find good idea to animate reloading UIScrollView content after removing one of the subview. As I thought about it there will be 4 different kinds of deletion:
1. deletion of last view
2. deletion of first view
3. deletion of middle view - hardest one
4. deletion of only view - easiest one, just animation of view disappearing
I need some suggestions/solutions.
Thanks,
Michal
edit:
Ok, I havent implemented it fully.
Here is reload method:
- (void)loadScrollView{
for(UIView* view in [_scrollView subviews])
{
[view removeFromSuperview];
}
CGRect frame = self.scrollView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
int i = 0;
_scrollView.clipsToBounds = NO;
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
for(MPContact* contact in self.items)
{
frame.origin.x = self.scrollView.frame.size.width*i;
MyView *view = [[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0];
[view setFrame:frame];
i++;
[view.nameAndSurnameLabel setText:#"view text"];
[view setDelegate:self];
[self.scrollView addSubview:view];[self.viewsArray addObject:view];
}[self.scrollView setContentSize:CGSizeMake(frame.size.width*[self.items count], frame.size.height)];}
Each MyView have delete button with delegate, delegate removes view from viewsArray and run this method for now.
I suggest you to use + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion method of UIView class. Just get your UIView that you want to delete, from UIScrollView and set view.alpha = 0 in animations block. In completion block remove this UIView from UIScrollView, using removeFromSuperView method. Setting alpha to 0 will make fade animation.
EDIT:
Iterate all your views you want to move and change it's x coordinate.
for(UIView *view in scrollview.subviews)
{
[UIView animateWithDuration:0.1 animations:^{
[view setFrame:CGRectMake(view.x-(/*HERE YOU NEED TO PUT WIDTH OF TOUR DELETING VIEW*/),view.y,view.width,view.heigh)];
}
completion:^(BOOL finished){
//remove view from superview and change your uiscrollview's content offset.
}];
}

UIScrollView metro theme

I am attempting to create a "metro" styled UIScrollView. It is similar to how iTunes app handles panels in the new ios version which wont be named.
I can't figure out how to have my views layout/scroll so that the next view in the sequence shows up. I've tried all sorts of things like keeping the contentSize the screen width but moving each view over -10ish so it will show up like above. I've tried making scrollView whose bounds were smaller than the screen so it would show the part of the next view. Nothing works.
Here is diagram of what I'm trying to do:
It seems extremely trivial on paper but I can't seem to get it work.
I'm not sure if I'm misinterpreting your requirements - but this might be a starting point to see how you could set it up:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewBounds = self.view.bounds;
CGRect scrollViewFrame = CGRectMake(0, 0, floorf(CGRectGetWidth(viewBounds) / 2.2), CGRectGetHeight(viewBounds));
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.center = self.view.center;
scrollView.contentSize = CGSizeMake(CGRectGetWidth(viewBounds) * 3, CGRectGetHeight(viewBounds) * 3);
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = NO;
UIPanGestureRecognizer *gestureRecognizer = scrollView.panGestureRecognizer;
[self.view addGestureRecognizer:gestureRecognizer];
for (int i = 0; i < 3; i++) {
CGRect frame = CGRectMake(10.f + (i * CGRectGetWidth(scrollView.bounds)), 10.f, CGRectGetWidth(scrollView.bounds) - 20.f, (CGRectGetHeight(scrollViewFrame) * 3) - 20.f);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor redColor];
[scrollView addSubview:view];
}
[self.view addSubview:scrollView];
}
Literally just put this in an empty viewController's viewDidLoad:
The key things to note are
contentSize needs to be wide enough for all the panels
clipsToBounds should be NO so you can see the additional views
The bounds of the scrollview is essentially the main view port
pagingEnabled should be set
I've grabbed the panGestureRecognizer from the scrollview and attached it to the containing view instead so that panning is detected in the bounds of the containing view (which is larger) otherwise you are restricted to only detecting scrolls within the scrollviews bounds

MKMapView panning causes parent UIScrollView to scroll

I have a MKMapView added to a UIScrollView. I thought that the expected functionality would be that panning the map would cause the map to move, but it's parent UIScrollView would not scroll. i.e. if you were panning the map, the scrollview would stay in it's current position.
This is my code:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[self.view addSubview:scrollView];
double mapHeight = 200;
if([AppDelegate isRunningOniPad])
mapHeight = 400;
map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 200, self.width, mapHeight)];
[map setExclusiveTouch:NO];
[scrollView map];
I've tried setting exclusive touch to YES and to NO, but it doesn't seem to have any effect.
Is there a way to get the behaviour working so that panning the map will stop the UIScrollView from moving? I've tried changing map to a UIScrollView instead of an MKMapView too, but that had the same behaviour - so I doubt that this is MKMapView specific.
The expected outcome is correct, the scroll view should remain stationary when the Map View is touched, as this should receive the touch events before they are passed up the responder chain to the scroll view.
I have quickly tested the above code in an empty project, and this is working as expected with a couple of alterations to the creating of frame & sizes, iPad size detection, and adding the map as a sub-view of the scroll view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
// Make the content size large to allow scroll view to move
[scrollView setContentSize:CGSizeMake(1000, 2000)];
[self.view addSubview:scrollView];
double mapHeight = 200;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
mapHeight = 400;
}
MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, mapHeight)];
[map setExclusiveTouch:NO];
[scrollView addSubview:map];
In case it helps anyone,
Adding a MKMapView to a UIScrollView should work as expected. In my situation, it wasn't working because my view hierarchy was setup incorrectly.
If you're having problems look at how you are adding sub views (ie [self.view addSubview:mapView]. Make sure the MKMapView is properly in the UIScrollView's hierarchy.
Also, I really like revealapp.com to inspecting the view hierarchy (and no I'm not associated with them ;)