tvOS Gesture Recognizer stops working after sliding offscreen - uigesturerecognizer

I have a view that acts like the macOS dock - it can be moved offscreen entirely.
My gesture recognizer looks like:
-(void)swipeDown:(UISwipeGestureRecognizer *)sender
{
NSLog(#"Swipe Down");
// this should move the dock 10 pixels below the bottom of the screen
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{[self dockView].frame = CGRectMake(kSafeMarginHorizontal, self.view.frame.size.height + 10, self.view.frame.size.width - (kSafeMarginHorizontal * 2), 80);}
completion:nil];
}
I am only using an autoresizing mask on my dockView with right and left edges pinned. In my main parent view I call:
[[self view] setTranslatesAutoresizingMaskIntoConstraints:YES];
This works fine, but after sliding offscreen, the corresponding Swipe Up gesture no longer works and if I swipe down again, I no longer get the NSLog indicating the method was called.
I can prevent this by not sliding the view entirely offscreen. If I leave a least a few pixels on the screen, it continues to work ok.
Why does this break my gesture recognizer?

It seems that tvOS does not like it when a focused button ends up offscreen. I have also changed this to animate by changing constraints. The key is calling setNeedsFocusUpdate at the end of the animation.
// flush pending layout operations, then animate the constraint change
[[self view] layoutIfNeeded];
[UIView animateWithDuration:0.25
delay:0.0
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
animations:^{[[self dockViewConstraint] setConstant:1080];
[[self view] layoutIfNeeded];}
completion:^(BOOL finished){
// Do some cleanup after animating.
[self setNeedsFocusUpdate];}];

Related

iOS8: What's going on with moving views during keyboard transitions?

After switching to iOS8, I'm getting weird behavior when I move views during a keyboard transition. Can anyone explain what's going on?
Here's a minimal example to demonstrate the problem. I have a simple view with a UITextField and a UIButton. The function nudgeUp moves the text field and the button up by 10 points. It is triggered either by the buttonPressed callback, or the keyboardWillShow callback.
When I tap the button, the code works as expected: buttonPressed calls nudgeUp and the button and text field jump up by 10 points.
When I tap the text field, keyboardWillShow calls nudgeUp, but the behaviour is very different. The button and text field immediately jump down by 10 points, and then slide back up to their original position as the keyboard shows itself.
Why is this happening? How can I regain control of animations during keyboard presentation in iOS8?
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Called when the keyboard appears.
[self nudgeUp];
}
- (IBAction)buttonPressed:(id)sender {
[self nudgeUp];
}
- (void)nudgeUp
{
CGRect newTextFieldFrame = self.textField.frame;
newTextFieldFrame.origin.y -= 10;
self.textField.frame = newTextFieldFrame;
CGRect newButtonFrame = self.button.frame;
newButtonFrame.origin.y -= 10;
self.button.frame = newButtonFrame;
}
#end
It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change can be animated.
Example:
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.bottomSpaceConstraint.constant = adjustmentedValue;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
You should use UIKeyboardDidShowNotification (you're using will version) and everything will work as you expect:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Called when the keyboard finished showing up
[self nudgeUp];
}
The explanation is that with UIKeyboardWillShowNotification you are changing the frames too early. After your changes the system will relayout everything to accomodate the keyboard and your changes won't have any effect.
Also, I recommend you to switch to autolayout and forget about frames.
Try using the UIKeyboardWillShowNotification userInfo to give you the frame of the keyboard. Then move the onscreen elements based on that.

Mac OSX - Core Animation - how to animate something which is following my cursor?

So far I have only seen Core Animation has code that looks like this, where the parameters of where to animate to are set in the beginning.
- (void) startTopLeftImageViewAnimation{
/* Start from top left corner */
[self.xcodeImageView1 setFrame:CGRectMake(0.0f,0.0f, 100.0f, 100.0f)];
[self.xcodeImageView1 setAlpha:1.0f];
[UIView beginAnimations:#"xcodeImageView1Animation" context:(__bridge void *)self.xcodeImageView1];
/* 3 seconds animation */
[UIView setAnimationDuration:3.0f];
/* Receive animation delegates */ [UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(imageViewDidStop:finished:context:)];
/* End at the bottom right corner */ [self.xcodeImageView1 setFrame:CGRectMake(220.0f,
350.0f, 100.0f,
[self.xcodeImageView1 setAlpha:0.0f];
[UIView commitAnimations];
}
However, I would like to make an animation in which the graphic follows my mouse cursor. This means that the animation is constantly receiving my mouse-pointer x-y values.
Is there a better alternative to doing "manual" animation like this, and calling drawRect whenever I shift the position of my mouse? Can I use Core Animation for this?
- (void)drawRect:(NSRect)rect
{
// Drawing code here.
[[NSColor whiteColor] set];
NSRectFill( rect );
[self drawCircleAtPoint:_circlePt];
}
- (void) drawCircleAtPoint:(CGPoint) point {
NSBezierPath* thePath = [NSBezierPath bezierPath];
NSRect nrect = NSMakeRect(point.x, point.y, 50, 50);
[thePath appendBezierPathWithOvalInRect:nrect];
[[NSColor blackColor] set];
[thePath stroke];
}
Thanks for any help regarding this.
First you would need the point where the mouse cursor is pointing to. The method you have to implement for this is
- (void)mouseMoved:(NSEvent *)theEvent
NSEvent has a method called
- (NSPoint)locationInWindow
which gives you, as you might guessed, the position of the cursor in the window.
Now you have to find the location within your view. This is done by NSView method
- (NSPoint)convertPoint:(NSPoint)aPoint fromView:(NSView *)aView
where the point comes from locationInWindow and the view is the view in which you want to perform the animation
e.g.
CGPoint mousePoint = [[[self window] contentView] convertPoint:[theEvent locationInWindow] fromView:self];
Then you can either set the position via the views frame or you can use the position property of the views layer.

Two Finger Drag with IKImageView and NSScrollView in Mountain Lion

I have a Mac App that's been in the app store for a year or so now. It was first published with target SDK 10.7, Lion. Upon the update to Mountain Lion it no longer works.
The application displays large images in an IKImageView which is embedded in an NSScrollView. The purpose of putting it into a scrollview was to get two finger dragging working, rather than the user having to click to drag. Using ScrollViewWorkaround by Nicholas Riley, I was able to use two finger scrolling to show the clipped content after the user had zoomed in. Just like you see in the Preview app.
Nicholas Riley's Solution:
IKImageView and scroll bars
Now in Mountain Lion this doesn't work. After zooming in, pinch or zoom button, the image is locked in the lower left portion of the image. It won't scroll.
So the question is, what's the appropriate way to display a large image in IKImageView and have two finger dragging of the zoomed image?
Thank you,
Stateful
Well, Nicholas Riley's Solution is an ugly hack in that it addresses the wrong class; the issue isn't with NSClipView (which he subclassed, but which works just fine as is), but with IKImageView.
The issue with IKImageView is actually quite simple (God knows why Apple hasn't fixed this in what? … 7 years ...): Its size does not adjust to the size of the image it displays. Now, when you embed an IKImageView in an NSScrollView, the scroll view obviously can only adjust its scroll bars relative to the size of the embedded IKImageView, not to the image it contains. And since the size of the IKImageView always stays the same, the scroll bars won't work as expected.
The following code subclasses IKImageView and fixes this behavior. Alas, it won't fix the fact that IKImageView is crash-prone in Mountain Lion as soon as you zoom …
///////////////////// HEADER FILE - FixedIKImageView.h
#import <Quartz/Quartz.h>
#interface FixedIKImageView : IKImageView
#end
///////////////////// IMPLEMENTATION FILE - FixedIKImageView.m
#import "FixedIKImageView.h"
#implementation FixedIKImageView
- (void)awakeFromNib
{
[self setTranslatesAutoresizingMaskIntoConstraints:NO]; // compatibility with Auto Layout; without this, there could be Auto Layout error messages when we are resized (delete this line if your app does not use Auto Layout)
}
// FixedIKImageView must *only* be used embedded within an NSScrollView. This means that setFrame: should never be called explicitly from outside the scroll view. Instead, this method is overwritten here to provide the correct behavior within a scroll view. The new implementation ignores the frameRect parameter.
- (void)setFrame:(NSRect)frameRect
{
NSSize imageSize = [self imageSize];
CGFloat zoomFactor = [self zoomFactor];
NSSize clipViewSize = [[self superview] frame].size;
// The content of our scroll view (which is ourselves) should stay at least as large as the scroll clip view, so we make ourselves as large as the clip view in case our (zoomed) image is smaller. However, if our image is larger than the clip view, we make ourselves as large as the image, to make the scrollbars appear and scale appropriately.
CGFloat newWidth = (imageSize.width * zoomFactor < clipViewSize.width)? clipViewSize.width : imageSize.width * zoomFactor;
CGFloat newHeight = (imageSize.height * zoomFactor < clipViewSize.height)? clipViewSize.height : imageSize.height * zoomFactor;
[super setFrame:NSMakeRect(0, 0, newWidth - 2, newHeight - 2)]; // actually, the clip view is 1 pixel larger than the content view on each side, so we must take that into account
}
//// We forward size affecting messages to our superclass, but add [self setFrame:NSZeroRect] to update the scroll bars. We also add [self setAutoresizes:NO]. Since IKImageView, instead of using [self setAutoresizes:NO], seems to set the autoresizes instance variable to NO directly, the scrollers would not be activated again without invoking [self setAutoresizes:NO] ourselves when these methods are invoked.
- (void)setZoomFactor:(CGFloat)zoomFactor
{
[super setZoomFactor:zoomFactor];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToRect:(NSRect)rect
{
[super zoomImageToRect:rect];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomIn:(id)sender
{
[super zoomIn:self];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomOut:(id)sender
{
[super zoomOut:self];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToActualSize:(id)sender
{
[super zoomImageToActualSize:sender];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToFit:(id)sender
{
[self setAutoresizes:YES]; // instead of invoking super's zoomImageToFit: method, which has problems of its own, we invoke setAutoresizes:YES, which does the same thing, but also makes sure the image stays zoomed to fit even if the scroll view is resized, which is the most intuitive behavior, anyway. Since there are no scroll bars in autoresize mode, we need not add [self setFrame:NSZeroRect].
}
- (void)setAutoresizes:(BOOL)autoresizes // As long as we autoresize, make sure that no scrollers flicker up occasionally during live update.
{
[self setHasHorizontalScroller:!autoresizes];
[self setHasVerticalScroller:!autoresizes];
[super setAutoresizes:autoresizes];
}
#end

UIButton Animation for IPhone

I am new to xcode, but I have been searching around for solution, and I have tried many different methods, but none of it is working. I am trying to make UIButton flip to the reverse side by adding animation to it. This will occur when program loads up, so user can just sit back and watch a whole bunch of buttons flip over and over.
The problem that I am having is that the animation seems to take place before screen even loads because I can see a different view (to show that button is flipped), as soon as the program loads, but I cannot see the animation taking place.
What I have done:
I created a main view controller and inside the controller I have different subviews and within those subviews, I have UIButtons. I tried to add a delay to my animation, but it still does not fix it. I don't know if the problem could occur because I create subviews and buttons and then animate under - (void)viewDidLoad. I also tried to create another subview and put just one button into that subview, which will create 2 subviews and each having just one button and animate by flipping between the subviews, but I still get the same problem. The animation still happened before the screen even loads up, instead of during the run time of the program.
I am attaching part of the code that I did to this one.
CGRect button20subviewFrame = CGRectMake(242, 370, 70, 83);
UIView *button20subview = [[UIView alloc] initWithFrame:button20subviewFrame];
UIButton *button20 = [UIButton buttonWithType:UIButtonTypeCustom];
[button20 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
[button20 setTitle:#"Button20" forState:UIControlStateNormal];
button20.frame = CGRectMake(0 , 0 , 70 , 83);
UIImage *button20Image = [UIImage imageNamed:#"t1.png"];
[button20 setImage:button20Image forState:UIControlStateNormal];
[button20subview addSubview:button20];
[self.view addSubview:button20subview];
UIButton *button21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button21 addTarget:self action:#selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
button21.frame = CGRectMake(0, 0, 70, 83);
UIImage *button21Image = [UIImage imageNamed:#"t2.png"];
[button21 setImage:button21Image forState:UIControlStateNormal];
//animation part
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
[button20 removeFromSuperview];
[button20subview addSubview:button21];
}completion:^(BOOL finished){
}];
Any help is appreciated.
Thank you.
viewDidLoad is the problem. It gets called when the view is done loading, which is before it is put on screen. viewDidAppear should do what you're looking for.
In addition to this, you may want to call [button20 removeFromSuperview] in the animations completion handler to ensure that the front of the button isn't removed while the animation is still taking place.
E.x:
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
    [button20subview addSubview:button21];
}completion:^(BOOL finished){
if(finished){
  [button20 removeFromSuperview];
}
}];

Removing superview is already done before animation

I have an app where a movie view is a kind of loading screen, and it's on top of my root controller, splitViewController. When the movie has finished, i want to remove it from the superview, animated. I'm using this code now, where mpmctr my movie controller is:
[UIView beginAnimations:#"blablablab" context:NULL];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:splitViewController.view.superview cache:NO];
[UIView setAnimationDuration:.5];
[mpMCtr.view removeFromSuperview];
[UIView commitAnimations];
When this code runs, mpmctr removes itself from the superview but not animated. This is happening when the splitviewcontroller is already on the screen.
Im using this code for putting mpmctr on the view in the delegate method didfinishlaunching.
[window addSubview:splitViewController.view];
[splitViewController.view addSubview:mpMCtr.view];
I hope that you guys can help me with this problem,
Thanks in advance.
A UIView animation can't animate removal from superview, but you could for example animate its alpha down to zero, then you could do this to remove the view after your animation has completed.
[UIView setAnimationDidStopSelector:#selector(removeMyView)];
- (void) removeMyView
{
[mpMCtr.view removeFromSuperview];
}
Despite what MDT says, you can actually animate the removal of a view with UIView animation. You just have to use the block-based API that was introduced in iOS 4.
This is the exact sample code from the Apples documentation for transitionWithView:duration:options:animations:completion:
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView]; }
completion:NULL];
It will flip from left, removing fromView and adding toView to containerView (the view that they are added removed from).

Resources