+
+- (void)dealloc
+{
+ [_saverName release];
+ // iOS: When a UIView deallocs, it doesn't do [UIView removeFromSuperView]
+ // for its subviews, so the subviews end up with a dangling pointer in their
+ // superview properties.
+ [aboutBox removeFromSuperview];
+ [aboutBox release];
+ [_saverView removeFromSuperview];
+ [_saverView release];
+ [super dealloc];
+}
+
+
+- (void)loadView
+{
+ // The UIViewController's view must never change, so it gets set here to
+ // a plain black background.
+
+ // This background view doesn't block the status bar, but that's probably
+ // OK, because it's never on screen for more than a fraction of a second.
+ UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectNull];
+ backgroundView.backgroundColor = [UIColor blackColor];
+ self.view = backgroundView;
+ [backgroundView release];
+}
+
+
+- (void)aboutPanel:(UIView *)saverView
+ orientation:(UIInterfaceOrientation)orient
+{
+ if (!_showAboutBox)
+ return;
+
+ NSString *name = _saverName;
+ NSString *year = [_parent makeDesc:_saverName yearOnly:YES];
+
+
+ CGRect frame = [saverView frame];
+ CGFloat rot;
+ CGFloat pt1 = 24;
+ CGFloat pt2 = 14;
+ UIFont *font1 = [UIFont boldSystemFontOfSize: pt1];
+ UIFont *font2 = [UIFont italicSystemFontOfSize:pt2];
+
+# ifdef __IPHONE_7_0
+ CGSize s = CGSizeMake(frame.size.width, frame.size.height);
+ CGSize tsize1 = [[[NSAttributedString alloc]
+ initWithString: name
+ attributes:@{ NSFontAttributeName: font1 }]
+ boundingRectWithSize: s
+ options: NSStringDrawingUsesLineFragmentOrigin
+ context: nil].size;
+ CGSize tsize2 = [[[NSAttributedString alloc]
+ initWithString: name
+ attributes:@{ NSFontAttributeName: font2 }]
+ boundingRectWithSize: s
+ options: NSStringDrawingUsesLineFragmentOrigin
+ context: nil].size;
+# else // iOS 6 or Cocoa
+ CGSize tsize1 = [name sizeWithFont:font1
+ constrainedToSize:CGSizeMake(frame.size.width,
+ frame.size.height)];
+ CGSize tsize2 = [year sizeWithFont:font2
+ constrainedToSize:CGSizeMake(frame.size.width,
+ frame.size.height)];
+# endif
+
+ CGSize tsize = CGSizeMake (tsize1.width > tsize2.width ?
+ tsize1.width : tsize2.width,
+ tsize1.height + tsize2.height);
+
+ tsize.width = ceilf(tsize.width);
+ tsize.height = ceilf(tsize.height);
+
+ // Don't know how to find inner margin of UITextView.
+ CGFloat margin = 10;
+ tsize.width += margin * 4;
+ tsize.height += margin * 2;
+
+ if ([saverView frame].size.width >= 768)
+ tsize.height += pt1 * 3; // extra bottom margin on iPad
+
+ frame = CGRectMake (0, 0, tsize.width, tsize.height);
+
+ /* Get the text oriented properly, and move it to the bottom of the
+ screen, since many savers have action in the middle.
+ */
+ switch (orient) {
+ case UIInterfaceOrientationLandscapeLeft:
+ rot = -M_PI/2;
+ frame.origin.x = ([saverView frame].size.width
+ - (tsize.width - tsize.height) / 2
+ - tsize.height);
+ frame.origin.y = ([saverView frame].size.height - tsize.height) / 2;
+ break;
+ case UIInterfaceOrientationLandscapeRight:
+ rot = M_PI/2;
+ frame.origin.x = -(tsize.width - tsize.height) / 2;
+ frame.origin.y = ([saverView frame].size.height - tsize.height) / 2;
+ break;
+ case UIInterfaceOrientationPortraitUpsideDown:
+ rot = M_PI;
+ frame.origin.x = ([saverView frame].size.width - tsize.width) / 2;
+ frame.origin.y = 0;
+ break;
+ default:
+ rot = 0;
+ frame.origin.x = ([saverView frame].size.width - tsize.width) / 2;
+ frame.origin.y = [saverView frame].size.height - tsize.height;
+ break;
+ }
+
+ if (aboutBox) {
+ [aboutBox removeFromSuperview];
+ [aboutBox release];
+ }
+
+ aboutBox = [[UIView alloc] initWithFrame:frame];
+
+ aboutBox.transform = CGAffineTransformMakeRotation (rot);
+ aboutBox.backgroundColor = [UIColor clearColor];
+
+ /* There seems to be no easy way to stroke the font, so instead draw
+ it 5 times, 4 in black and 1 in yellow, offset by 1 pixel, and add
+ a black shadow to each. (You'd think the shadow alone would be
+ enough, but there's no way to make it dark enough to be legible.)
+ */
+ for (int i = 0; i < 5; i++) {
+ UITextView *textview;
+ int off = 1;
+ frame.origin.x = frame.origin.y = 0;
+ switch (i) {
+ case 0: frame.origin.x = -off; break;
+ case 1: frame.origin.x = off; break;
+ case 2: frame.origin.y = -off; break;
+ case 3: frame.origin.y = off; break;
+ }
+
+ for (int j = 0; j < 2; j++) {
+
+ frame.origin.y = (j == 0 ? 0 : pt1);
+ textview = [[UITextView alloc] initWithFrame:frame];
+ textview.font = (j == 0 ? font1 : font2);
+ textview.text = (j == 0 ? name : year);
+ textview.textAlignment = NSTextAlignmentCenter;
+ textview.showsHorizontalScrollIndicator = NO;
+ textview.showsVerticalScrollIndicator = NO;
+ textview.scrollEnabled = NO;
+ textview.editable = NO;
+ textview.userInteractionEnabled = NO;
+ textview.backgroundColor = [UIColor clearColor];
+ textview.textColor = (i == 4
+ ? [UIColor yellowColor]
+ : [UIColor blackColor]);
+
+ CALayer *textLayer = (CALayer *)
+ [textview.layer.sublayers objectAtIndex:0];
+ textLayer.shadowColor = [UIColor blackColor].CGColor;
+ textLayer.shadowOffset = CGSizeMake(0, 0);
+ textLayer.shadowOpacity = 1;
+ textLayer.shadowRadius = 2;
+
+ [aboutBox addSubview:textview];
+ }
+ }
+
+ CABasicAnimation *anim =
+ [CABasicAnimation animationWithKeyPath:@"opacity"];
+ anim.duration = 0.3;
+ anim.repeatCount = 1;
+ anim.autoreverses = NO;
+ anim.fromValue = [NSNumber numberWithFloat:0.0];
+ anim.toValue = [NSNumber numberWithFloat:1.0];
+ [aboutBox.layer addAnimation:anim forKey:@"animateOpacity"];
+
+ [saverView addSubview:aboutBox];
+
+ if (splashTimer)
+ [splashTimer invalidate];
+
+ splashTimer =
+ [NSTimer scheduledTimerWithTimeInterval: anim.duration + 2
+ target:self
+ selector:@selector(aboutOff)
+ userInfo:nil
+ repeats:NO];
+}
+
+
+- (void)aboutOff
+{
+ [self aboutOff:FALSE];
+}
+
+- (void)aboutOff:(BOOL)fast
+{
+ if (aboutBox) {
+ if (splashTimer) {
+ [splashTimer invalidate];
+ splashTimer = 0;
+ }
+ if (fast) {
+ aboutBox.layer.opacity = 0;
+ return;
+ }
+
+ CABasicAnimation *anim =
+ [CABasicAnimation animationWithKeyPath:@"opacity"];
+ anim.duration = 0.3;
+ anim.repeatCount = 1;
+ anim.autoreverses = NO;
+ anim.fromValue = [NSNumber numberWithFloat: 1];
+ anim.toValue = [NSNumber numberWithFloat: 0];
+ // anim.delegate = self;
+ aboutBox.layer.opacity = 0;
+ [aboutBox.layer addAnimation:anim forKey:@"animateOpacity"];
+ }
+}
+
+
+- (void)createSaverView
+{
+ UIView *parentView = self.view;
+
+ if (_saverView) {
+ [_saverView removeFromSuperview];
+ [_saverView release];
+ }
+
+# if 0
+ if (_storedOrientation != UIInterfaceOrientationUnknown) {
+ [[UIApplication sharedApplication]
+ setStatusBarOrientation:_storedOrientation
+ animated:NO];
+ }
+# endif
+
+ _saverView = [_parent newSaverView:_saverName
+ withSize:parentView.bounds.size];
+
+ if (! _saverView) {
+ UIAlertController *c = [UIAlertController
+ alertControllerWithTitle:@"Unable to load!"
+ message:@""
+ preferredStyle:UIAlertControllerStyleAlert];
+ [c addAction: [UIAlertAction actionWithTitle: @"Bummer"
+ style: UIAlertActionStyleDefault
+ handler: ^(UIAlertAction *a) {
+ // #### Should expose the SaverListController...
+ }]];
+ [self presentViewController:c animated:YES completion:nil];
+
+ return;
+ }
+
+ _saverView.delegate = _parent;
+ _saverView.autoresizingMask =
+ UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+
+ [self.view addSubview:_saverView];
+
+ // The first responder must be set only after the view was placed in the view
+ // heirarchy.
+ [_saverView becomeFirstResponder]; // For shakes on iOS 6.
+ [_saverView startAnimation];
+ [self aboutPanel:_saverView
+ orientation:/* _storedOrientation */ UIInterfaceOrientationPortrait];
+}
+
+
+- (void)viewDidAppear:(BOOL)animated
+{
+ [super viewDidAppear:animated];
+ [self createSaverView];
+}
+
+
+- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)o
+{
+ return NO; /* Deprecated in iOS 6 */
+}
+
+
+- (BOOL)shouldAutorotate /* Added in iOS 6 */
+{
+ return
+ NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0 ?
+ ![_saverView suppressRotationAnimation] :
+ YES;
+}
+
+
+- (UIInterfaceOrientationMask)supportedInterfaceOrientations /* Added in iOS 6 */
+{
+ // Lies from the iOS docs:
+ // "This method is only called if the view controller's shouldAutorotate
+ // method returns YES."
+ return UIInterfaceOrientationMaskAll;
+}
+
+
+/*
+- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
+{
+ return UIInterfaceOrientationPortrait;
+}
+*/
+
+
+- (void)setSaverName:(NSString *)name
+{
+ [name retain];
+ [_saverName release];
+ _saverName = name;
+ // _storedOrientation =
+ // [UIApplication sharedApplication].statusBarOrientation;
+
+ if (_saverView)
+ [self createSaverView];
+}
+
+
+- (void)viewWillTransitionToSize: (CGSize)size
+ withTransitionCoordinator:
+ (id<UIViewControllerTransitionCoordinator>) coordinator
+{
+ [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
+
+ if (!_saverView)
+ return;
+
+ [CATransaction begin];
+
+ // Completely suppress the rotation animation, since we
+ // will not (visually) be rotating at all.
+ if ([_saverView suppressRotationAnimation])
+ [CATransaction setDisableActions:YES];
+
+ [self aboutOff:TRUE]; // It does goofy things if we rotate while it's up
+
+ [coordinator animateAlongsideTransition:^
+ (id <UIViewControllerTransitionCoordinatorContext> context) {
+ // This executes repeatedly during the rotation.
+ } completion:^(id <UIViewControllerTransitionCoordinatorContext> context) {
+ // This executes once when the rotation has finished.
+ [CATransaction commit];
+ [_saverView orientationChanged];
+ }];
+ // No code goes here, as it would execute before the above completes.
+}
+