How to create a paging UIScrollView with "oversized" pages - iphone

Is there a suggested way to create a paging UIScrollView that has pages wider than the bounds of the UISrollView?
I would need something like this.
normal scrolling within page2 and paging mode with the "rubberband" effect on the edges of the pages.
The paging effect looks a bit complicated for me, if you flick fast you go to the next page, if you slide slow you see the new page at the edge and only after a certain point the page is changed.
Maybe somebody can shed some light on the way to handle this, is this even possible with the sole use of UIScrollViewDelegate methods or do I have to subclass?

I'm impressed. This was actually much much easier than I thought in the beginning.
The simple solution was to encapsulate each page in a non-paging scrollview. And done. No need to implement UIScrollViewDelegate, no need to subclass. Three extra lines of code
For the regular sized pages I had something like this:
UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[mainScroller addSubview:myCustomView];
totalWidth += width;
and now I have this:
UIView *myCustomView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, bigWidth, height)] autorelease];
UIScrollView *secondaryScroller = [[[UIScrollView alloc] initWithFrame:CGRectMake(totalWidth, 0, width, height)] autorelease];
[secondaryScroller setContentSize:myCustomView.frame.size];
[secondaryScroller addSubview:myCustomView];
[mainScroller addSubview:secondaryScroller];
totalWidth += width;
Three lines. Amazing.
The view hierarchy:
<UIScrollView: 0x4b32eb0; frame = (0 0; 768 1004); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x4b32d00>; contentOffset: {0, 0}>
| <UIScrollView: 0x4b32710; frame = (0 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b35580>; contentOffset: {0, 0}>
| | <UIView: 0x4b33f70; frame = (0 0; 1352 1004); layer = <CALayer: 0x4b16c20>>
| <UIScrollView: 0x4b34790; frame = (768 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33e10>; contentOffset: {0, 0}>
| | <UIView: 0x4b30fa0; frame = (0 0; 789 1004); layer = <CALayer: 0x4b329f0>>
| <UIScrollView: 0x4b34920; frame = (1536 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b33180>; contentOffset: {0, 0}>
| | <UIView: 0x4b30d00; frame = (0 0; 1398 1004); layer = <CALayer: 0x4b33120>>
| <UIScrollView: 0x4b31fe0; frame = (2304 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b32170>; contentOffset: {0, 0}>
| | <UIView: 0x4b34c50; frame = (0 0; 863 1004); layer = <CALayer: 0x4b31f80>>
| <UIScrollView: 0x4b32460; frame = (3072 0; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x4b325f0>; contentOffset: {0, 0}>
| | <UIView: 0x4b323d0; frame = (0 0; 1064 1004); layer = <CALayer: 0x4b32400>>

I used this tutorial -
http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/
if you want to make a larger page, you can increase the size of view of PageControlExampleViewControl in this tutorial. Lets say make its width to 360 instead of default 320.

As far as I know, there is no way of achieving this directly by using the scrollviews paging property.
You would have to implement your own UIScrollView subclass and within your implementation file you would need to implement:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
Work out how much the scroll view has scrolled using the contentOffset property.
And make use of UIScrollViews's scrollRectToVisible: to implement your own custom scrolling functionality.
[self scrollRectToVisible:CGRectMake(horizontalScrollAmount,virticalScrollAmount,rectWidth,rectHeight) animated:NO];
The chain of events would be something like: record the location of a beginning touch, if the touch moves, find out which direction it moved by checking to see if its x/y coordinate is greater than or less than its starting position, if the touched moved a sufficient amount across the screen, then scroll the view by your designated paging size using scrollRectToVisible:.

Related

disable UIAppearance-API happen on RemoteViewControllers

So i am styling all my views using the apprearance api.
F.e. i style my UINavigationBar using:
[[UINavigationBar appearance] setBackgroundImage:navigationBarBgImage forBarMetrics:UIBarMetricsDefault];
i want to use the advantage of the appearance api of styling all my UINavigationBars in one single place (because i have multiple of them), so i don't want to do some subclassing only because of styling reasons.
i also popup some MFMessageComposeViewControllers and a SLComposeViewController to post to imessage or facebook.
my problem here is, if i try to select albums on facebook or select contacts on imessage modalview, this happens:
see the recursive description of the imessage modal view:
$0 = 0x1f1f1320 <UIWindow: 0x1e5c8900; frame = (0 0; 320 568); layer = <UIWindowLayer: 0x1e5c8a00>>
| <UILayoutContainerView: 0x1e592860; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x1e592910>>
| | <UINavigationTransitionView: 0x1f1c88a0; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x1f1c8960>>
| | | <UIViewControllerWrapperView: 0x1f1ee090; frame = (0 20; 320 548); autoresize = W+H; layer = <CALayer: 0x1f2f9560>>
| | | | <UIView: 0x1f2f3d20; frame = (0 0; 320 548); autoresize = W+H; layer = <CALayer: 0x1f2f3d80>>
| | | | | <_UISizeTrackingView: 0x1f2effd0; frame = (0 0; 320 548); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x1f2f00b0>>
| | | | | | <_UIRemoteView: 0x1f2f01e0; frame = (0 0; 320 568); transform = [0.5, -0, 0, 0.5, -0, 0]; userInteractionEnabled = NO; layer = <CALayerHost: 0x1f2f0330>>
any ideas whats the best to switch back there to default mode?
Your best bet is really to subclass one thing or the other. Otherwise you'll be undoing styles everywhere. I'll do it in one of two ways:
One is to subclass UINavigationController and style the UINavigationBar contained by that class:
UINavigationBar *navigationBarProxy = [UINavigationBar appearanceWhenContainedIn:[MyNavigationController class], nil]; //
[navigationBarProxy setBackgroundImage:navigationBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
// and so on
Another method is to subclass UINavigationBar instead, set the appearance for that class, and instantiate your UINavigationController this way:
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:nil];
// navigationController now has a navigationBar of your preferred type
Well, possible solution is set navigation bars to theirs original tint color with [UINavigationBar appearanceWhenContainedIn:[SLComposeViewController class]] ...
I hit the same problem and, worse, it was causing my app to crash for any of the XPC/UIRemoteView based controllers, including SLComposeViewController and MFMailComposeViewController.
My solution is to use this:
[[UINavigationBar appearanceWhenContainedIn:[RootViewController class], nil] setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
instead of:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
where RootViewController is whatever you've named the UIViewController that contains all of your app's subviews.
It doesn't work for everything (depends on how a controller ends up in the hierarchy) but, hey, it's something.

IOS activityIndicator subview above MKMapView

I have a mapView (_mapView) which in itself is a subView of my viewController , I would like to add an activityIndicator when I am fetching lots of pins, however I cannot get it to appear above the mapView, can anyone suggest why not
UIActivityIndicatorView *activityIndicator = nil;
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] ;
CGRect frame = activityIndicator.frame;
frame.origin = CGPointMake(290, 12);
activityIndicator.frame = frame;
activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
activityIndicator.tag = 2;
[self._mapView addSubview:activityIndicator];
dataMarkers = [[kepnService new] getPlacesByBounds:neCoord.latitude :neCoord.longitude :swCoord.latitude :swCoord.longitude]; // call to webservice
.... other processing code
[activityIndicator removeFromSuperview];
For information my view stack looks like this
subviews (
"<MKMapView: 0x6d6f320; frame = (0 0; 320 460); clipsToBounds = YES; layer = <CALayer: 0x6d99520>>",
"<UIToolbar: 0x6d93f70; frame = (-1 0; 342 44); autoresize = W+TM; layer = <CALayer: 0x6d8bbf0>>",
"<UINavigationBar: 0x6d9a4f0; frame = (0 416; 320 44); autoresize = W+BM; layer = <CALayer: 0x6d9a5b0>>"
)
Seems like you are adding the activity indicator, blocking the main thread and then removing it.
What you should do is:
Add the activity indicator.
Detach a thread or block to the background to perform the processing code.
When done, remove the activity indicator.

Updating subviews of a UIScrollView without updating the scroll handle

Okay so I have a subclassed UIScrollView with some subviews inside it, when the device orientation changes I want to update the position of the subviews inside so that they fit the new orientation.
When I just updated the positions of the subviews and then went to scroll I noticed that the scroll handle was ginormous... My loop that went through all of the subviews and updated their frames also set the frame of the scroll handle...
So to get around this.. whenever I added subviews I set a tag on them. When the orientation changed I had a condition around my set frame which checked if the tag on the view matched the subviews I added. If yes then set the frame...
int i = 0;
for (UIView *gridViewItem in self.subviews) {
if (gridViewItem.tag == GRID_CELL) {
gridViewItem.frame = [self calculateCellPosition:i];
}
i++;
}
But now I want to detect a touch on the subviews, and the easiest way I can think of doing that is by using the tag property so I know which subview is being tapped... However the tag is currently being used to help me distinguish the scroll handle from the other subviews...
When i logged out the subviews of my UIScrollView I got this:
2011-05-08 16:07:56.266 XXXXXX[2096:207] (
"<UIView: 0x5a0d3f0; frame = (16 16; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a09250>>",
"<UIView: 0x5a19d70; frame = (167 16; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a17660>>",
"<UIView: 0x5a1d4f0; frame = (16 137; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1d520>>",
"<UIView: 0x5a1e300; frame = (167 137; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1e330>>",
"<UIView: 0x5a1e4f0; frame = (16 258; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1e520>>",
"<UIView: 0x5a1f320; frame = (167 258; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a1f350>>",
"<UIView: 0x5a20120; frame = (16 379; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20150>>",
"<UIView: 0x5a20fb0; frame = (167 379; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20fe0>>",
"<UIView: 0x5a21dd0; frame = (16 500; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a21e00>>",
"<UIView: 0x5a22bb0; frame = (167 500; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a22be0>>",
"<UIView: 0x5a239a0; frame = (16 621; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a239d0>>",
"<UIView: 0x5a247b0; frame = (167 621; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a247e0>>",
"<UIView: 0x5a20f30; frame = (16 742; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a20f80>>",
"<UIView: 0x5a26460; frame = (167 742; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a26490>>",
"<UIView: 0x5a27220; frame = (16 863; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a27250>>",
"<UIView: 0x5a20dd0; frame = (167 863; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a28080>>",
"<UIView: 0x5a28e60; frame = (16 984; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a28e90>>",
"<UIView: 0x5a29c50; frame = (167 984; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a29c80>>",
"<UIView: 0x5a2aa60; frame = (16 1105; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2aa90>>",
"<UIView: 0x5a2b870; frame = (167 1105; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2b8a0>>",
"<UIView: 0x5a255a0; frame = (16 1226; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a255f0>>",
"<UIView: 0x5a262f0; frame = (167 1226; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2d610>>",
"<UIView: 0x5a2e3c0; frame = (16 1347; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2e3f0>>",
"<UIView: 0x5a2f1d0; frame = (167 1347; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2f200>>",
"<UIView: 0x5a2ffc0; frame = (16 1468; 135 105); clipsToBounds = YES; tag = 1; layer = <CALayer: 0x5a2fff0>>"
)
All of the subviews have my tag on it! I can't see the scroll handle? Does anyone know a way of identifying the scroll handle without using a tag like I have?
Your help will be much appreciated! Thanks!
I probably wouldn't sub-class UIScrollView for this. Add a single UIView to UIScrollView and add your cells to that view. Then always work with that view. No need for tags and doesn't interfere with UIScrollViews inner workings. Scrolling should work as long as the size of your content view reflects the cells inside it.

Bizarre UIScrollView behavior

I've got myself totally baffled with this scrollview. For some reason, on the third pass of this loop, an extra two UIImageViews get added as subviews. I assume i'm addressing bad memory, or something to that effect, but I can't figure out where it is coming from for the life of me.
Here's the code:
scrollview=[[[UIScrollView alloc] initWithFrame:CGRectMake(x, y+40, 300, 225)] retain];
NSArray *chunks=[lesson.photoString componentsSeparatedByString:#"|"];
ALAssetsLibrary *assetLibrary;
__block CGRect workingFrame = scrollview.bounds;
__block UIImageView *imageview=nil;
workingFrame.origin.x = 0;
NSLog(#"Chunks: %d\n",[chunks count]);
for(NSString *url in chunks) {
//
if(url && [url length])
{
NSURL *asseturl = [NSURL URLWithString:url];
assetLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLibrary assetForURL:asseturl
resultBlock:^(ALAsset *myasset)
{
UIView *sub=[[UIView alloc] initWithFrame:workingFrame];
imageview=[[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:[[myasset defaultRepresentation] fullScreenImage]]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = sub.bounds;
[sub addSubview:imageview];
[imageview release];
UIButton *imgfull=[UIButton buttonWithType:UIButtonTypeCustom];
[imgfull setFrame:sub.bounds];
imgfull.backgroundColor=[UIColor clearColor];
[imgfull addTarget:self action:#selector(imageViewToFullScreen) forControlEvents:UIControlEventTouchUpInside];
[sub addSubview:imgfull];
[imgfull release];
[scrollview addSubview: sub];
[sub release];
NSLog(#"%#",[scrollview subviews]);
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
if(scrollview.contentSize.width>300)
[scrollview flashScrollIndicators];
}
failureBlock:^(NSError *myerror){
NSLog(#"Failure - %#",[myerror localizedDescription]);
}
];
}
}
[scrollview setPagingEnabled:YES];
[self.view addSubview:scrollview];
Here's the output:
2011-05-04 11:53:00.331 AppName[24896:207] Chunks: 4
2011-05-04 11:53:00.369 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>"
)
2011-05-04 11:53:00.394 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>"
)
2011-05-04 11:53:00.426 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
"<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
"<UIImageView: 0x6605490; frame = (1 217; 149 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6686ff0>; }; layer = <CALayer: 0x6664280>>",
"<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>"
)
2011-05-04 11:53:00.439 AppName[24896:207] (
"<UIView: 0x6685960; frame = (0 0; 300 225); layer = <CALayer: 0x6685990>>",
"<UIView: 0x642db00; frame = (300 0; 300 225); layer = <CALayer: 0x6420c80>>",
"<UIImageView: 0x666e3d0; frame = (292 1; 7 223); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x666e400>>",
"<UIView: 0x6686c30; frame = (600 0; 300 225); layer = <CALayer: 0x6686c60>>",
"<UIImageView: 0x6605490; frame = (1 217; 99 7); opaque = NO; autoresize = TM; userInteractionEnabled = NO; animations = { opacity=<CABasicAnimation: 0x6648d60>; }; layer = <CALayer: 0x6664280>>",
"<UIView: 0x6673f90; frame = (900 0; 300 225); layer = <CALayer: 0x6673fc0>>"
)
EDIT: I should also mention that I've NSLog'd sub, and it produces the results I'd expect (1 imageview, 1 button per trip through the loop). I should also mention, that as far as I know I have no imageviews of the dimensions shown from the scrollview logging (99x7 or 7x223) anywhere in my app.
EDIT 2: A bit further information: I switched the add line to [scrollview addSubview:nil]; and I'm still seeing two imageviews being added on the third pass of the loop. Also, scrollview isn't accessed from anywhere else outside the posted code section.
EDIT 3: Figured out out. I'm flashing the scrollbars too early, which is mucking with the order of the subviews within the scrollview.
Are these the scroll view indicators flashing?
If no unusual images actually appear on your scroll view, you can assume those subviews are managed internally by UIScrollView and you should ignore them. Scroll indicators, perhaps.
Actually These extra two images are the uiscrollindicator images that are vertical scroll indicator and horizontal scroll indicator with tag 0 That are added by UIScrollview Automatically , if you add UIIMageViews to the ScrollView make sure to assign tag greater than zero, or use some other technique , so that you can retrieve your UIIMageViews by tag greater than zero..

Where Does A UIAlertView Live While Not Dismissed

Does anyone know in whose subview an active UIAlertView is located or how to find the thread in which it is running?
If you dump the contents of the windows property and all subviews of all views you can see that the UIAlertView is in a separate window that overlays the main window. Here I have a navbar with a viewcontroller and a tableview (I removed its subviews since they're not relevent).
<UIWindow: 0x411fd50; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x4120af0>>
: <UILayoutContainerView: 0x4123310; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x411f800>>
: | <UINavigationTransitionView: 0x4123500; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x41232e0>>
: | : <UIViewControllerWrapperView: 0x4519d30; frame = (0 64; 320 416); autoresize = W+H; layer = <CALayer: 0x4519a40>>
: | : | <UITableView: 0x7808000; frame = (0 0; 320 416); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x45182a0>>
: | <UINavigationBar: 0x45018b0; frame = (0 20; 320 44); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x4500fe0>>
: | : <UINavigationItemView: 0x4522a20; frame = (100 8; 160 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4526310>>
: | : <UINavigationItemButtonView: 0x45230a0; frame = (5 7; 87 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4520260>>
<_UIAlertOverlayWindow: 0x4179b70; frame = (0 0; 320 480); opaque = NO; layer = <CALayer: 0x4188dc0>>
: <UIAlertView: 0x4194bc0; frame = (3.8 161.95; 312.4 177.1); transform = [1.1, 0, 0, 1.1, 0, 0]; opaque = NO; animations = { transform=<CABasicAnimation: 0x4191160>; opacity=<CABasicAnimation: 0x41226f0>; }; layer = <CALayer: 0x4144c30>>
: | <UILabel: 0x4177e70; frame = (12 15; 260 23); text = 'Name of Date'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4179370>>
: | <UILabel: 0x418b100; frame = (12 45; 260 41); text = 'Name of the date that you...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4128450>>
: | <UIThreePartButton: 0x41942a0; frame = (11 102; 262 43); opaque = NO; tag = 1; layer = <CALayer: 0x4191f30>>
Here is the code that produces the dump. I found it useful on occasion to see what is going on when something isn't doing what I expect:
void dumpView(UIView* aView, NSString* indent) {
if (aView) {
NSLog(#"%#%#", indent, aView); // dump this view
if (aView.subviews.count > 0) {
NSString* subIndent = [[NSString alloc] initWithFormat:#"%#%#",
indent, ([indent length]/2)%2==0 ? #"| " : #": "];
for (UIView* aSubview in aView.subviews) dumpView( aSubview, subIndent );
[subIndent release];
}
}
}
void dumpWindows() {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
dumpView(window, #"dumpView: ");
}
}
The active UIAlertView lives in a separate window (_UIAlertOverlayWindow). Use .windows property to find it.
The whole UI runs in the main thread.
From the problem that you describe with windows and timing, sounds like you should implement alertView:didDismissWithButtonIndex:. You can trigger your followup code from within that method.
EDIT: if that did not work, I'd try doing a delay to execute the FB stuff after a delay when the window is really guaranteed to be gone.