Why my class Inheritance from UIDynamicAnimator can not use addChildBehavior - ios

I am learning ios with Stanford CS193P, and have issue in Lecture 8
I created a class named DropItBehavior inheritance from UIDynamicAnimator, but I can not use a UIDynamicAnimator method addChildBehavior,xcode warning like"No visible #interface for "DropItBehavior" declares the selector 'addChildBehavior'"
Here is the .h file
#import <UIKit/UIKit.h>
#interface DropItBehavior : UIDynamicAnimator
- (void)addItem:(id <UIDynamicItem>)item;
- (void)removeItem:(id <UIDynamicItem>)item;
#end
Here is the .m file
#import "DropItBehavior.h"
#interface DropItBehavior()
#property (strong, nonatomic) UIGravityBehavior *gravity;
#property (strong, nonatomic) UICollisionBehavior *collision;
#end
#implementation DropItBehavior
- (instancetype)init
{
self = [super init];
[self addChildBehavior:self.gravity];//Here is the Xcode warning"No visible #interface for "DropItBehavior" declares the selector 'addChildBehavior"
return self;
}
- (instancetype)init
{
self = [super init];
[self addChildBehavior:self.gravity];
return self;
}
- (UIGravityBehavior *)gravity
{
if (!_gravity) {
_gravity = [[UIGravityBehavior alloc] init];
_gravity.magnitude = 0.9;
}
return _gravity;
}
- (UICollisionBehavior *)collision
{
if (!_collision) {
_collision = [[UICollisionBehavior alloc] init];
_collision.translatesReferenceBoundsIntoBoundary = YES;
}
return _collision;
}
- (void)addItem:(id <UIDynamicItem>)item
{
[self.gravity addItem:item];
[self.collision addItem:item];
}
- (void)removeItem:(id <UIDynamicItem>)item
{
[self.gravity removeItem:item];
[self.collision removeItem:item];
}
#end

This is because addChildBehavior is a method of UIDynamicBehavior, not of UIDynamicAnimator. According to CS-193P's lecture notes, your DropIt class should inherit from UIDynamicBehavior instead:
#interface DropItBehavior : UIDynamicBehavior

Looking at the docs, there is no method -[UIDynamicAnimator addChildBehavior:], nor in your subclass.
However, UIDynamicBehavior does.
I would double check you are subclassing the right objects.

Related

How to layout parent of ASDisplayNode in AsyncDisplayKit?

i want to do like on this picture:
i do templateDisplayNode:
#interface TemplateDisplayNode : ASDisplayNode
#property (nonatomic, strong) ASDisplayNode *headerNode;
#property (nonatomic, strong) ASDisplayNode *ContentNode;
- (void)addContentNode:(ASDisplayNode *)contentNode;
#end
#implement TemplateDisplayNode
- (instancetype)init
{
self = [super init];
if (self) {
[self createSubviews];
}
return self;
}
- (void)createSubviews {
[self createHeaderNode];
[self createContentNode];
}
- (void)addContentNode:(ASDisplayNode *)contentNode {
[self.contentNode addSubnode:contentNode];
}
- (void)createHeaderNode {
self.headerNode = [[ASDisplayNode alloc] init];
self.headerNode.backgroundColor = [UIColor blueColor];
[self addSubnode:self.headerNode];
}
- (void)createContentNode {
self.contentNode = [[ASDisplayNode alloc] init];
self.contentNode.style.flexGrow = YES;
[self addSubnode:self.contentNode];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
CGFloat mainScreenWidth = [UIScreen mainScreen].bounds.size.width;
NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];
if (self.headerNode) {
self.headerNode.style.preferredSize = CGSizeMake(mainScreenWidth, 48);
[mainStackContent addObject:self.headerNode];
}
if (self.contentNode) {
[mainStackContent addObject:self.contentNode];
}
ASStackLayoutSpec *contentSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:mainStackContent];
return contentSpec;
}
#end
and create page of TemplateDisplayNode class:
#interface Page1Node : TemplateDisplayNode
#end
#implement Page1Node
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
[super layoutSpecThatFits:constrainedSize];
}
#end
And use it in ASViewController:
#interface MyViewController : ASViewController
#property (nonatomic, strong) MyCustomASViewControllerWithTableNode *myTableNodeViewController;
#property (nonatomic, strong) Page1Node *page1node;
#end
#implement MyViewController
- (instancetype)init {
self.page1node = [[Page1Node alloc] init];
self = [super initWithNode:self.page1node];
if (self) {
[self createMyTableNodeViewController];
}
return self;
}
- (void)createMyTableNodeViewController {
self.myTableNodeViewController = [[MyCustomASViewControllerWithTableNode alloc] init];
[self.page1node addContentNode:self.myTableNodeViewController.node]; //add TableNode like content on page1node
[self addChildViewController:self.myTableNodeViewController];
[self.myTableNodeViewController didMoveToParentViewController:self];
}
#end
MyCustomASViewControllerWithTableNode:
#interface MyCustomASViewControllerWithTableNode : ASViewController
#property (nonatomic, strong) ASTableNode *tableNode;
#end
...
- (instancetype)init {
_tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
self = [super initWithNode:_tableNode];
if (self) {
_tableNode.dataSource = self;
_tableNode.delegate = self;
}
return self;
}
and when i run it, I see a table in the content field, but I can not scroll the table, because Size frame ContentNode = {0; 0};
How to make layout correctly in order to set the size? If I do the same thing, but without using TemplateDisplayNode, then everything works. I would like to use TemplateDisplayNode to remove a lot of duplicate code for similar pages.
AsyncDisplayKit documentation is not advised to use:
[super layoutSpecThatFits:constrainedSize];
If I remove this line, then I do not go into layoutSpecThatFits in TemplateDisplayNode.
Advise how to be? What solutions can be?

Interactive Animated Transition between NavigationControllers

When presenting a new ViewController using the:
UINavigtionController *nav = [[UINavigationController alloc] initWithRoot(myVC)]
[self presentViewController:nav animated:YES completion:nil];
The UIViewControllerTransitionDelegate is never triggered since I am declaring this in myVC. My question is thus: how do I properly setup a interactive transition using the above initializiation?
EDIT:
I tried to follow the advice given below with no luck yet:
VC1.m :
LTFollowersListViewController *f = [LTFollowersListViewController new];
f.delegate = self;
LTNavigationController *nav = [[LTNavigationController alloc] initWithRootViewController:f];
[self presentViewController:nav animated:YES completion:nil];
VC2.h:
#interface LTFollowersListViewController : UIViewController <UIViewControllerTransitioningDelegate>
#property (nonatomic, strong) id<UIViewControllerTransitioningDelegate> delegate;
VC2.m:
#property (nonatomic, strong) CEPanAnimationController *animationController;
#property (nonatomic, strong) CEHorizontalSwipeInteractionController *interactionController;
in the viewDidLoad: {
self.delegate = self;
self.animationController = [[CEPanAnimationController alloc] init];
self.interactionController = [[CEHorizontalSwipeInteractionController alloc] init];
}
in the bottom of file:
#pragma mark - UIViewControllerTransitionsingDelegate
- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source {
NSLog(#"Reached the animation dismissal");
// allow the interaction controller to wire-up its gesture recognisers
[self.interactionController wireToViewController:self.navigationController forOperation:CEInteractionOperationDismiss];
self.animationController.reverse = NO;
return self.animationController;
}
- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForDismissedController:(UIViewController *)dismissed {
self.animationController.reverse = YES;
return self.animationController;
}
- (id<UIViewControllerInteractiveTransitioning>)
interactionControllerForDismissal:
(id<UIViewControllerAnimatedTransitioning>)animator {
NSLog(#"Reached the animation interaction");
// provide the interaction controller, if an interactive transition is in progress
return self.interactionController.interactionInProgress
? self.interactionController : nil;
}
Any suggestions as to what I am missing here? Thanks!
Did you miss
.h file declare the delegate and protocol ViewController will implement
#interface ViewController : UIViewController<UIViewControllerTransitioningDelegate>
#property (nonatomic, strong) id<UIViewControllerTransitioningDelegate> delegate;
.m file Setting the delegate
self.delegate = self;
and also implement the delegate methods.

What is the reason for UIgravity not working if not declared as a property?

Okay so i started learning UIDynamics and wanted to just work it out on small box.
I declared UIGravityBehaviour and UIDynamicsBehaviour in my ViewController.m and coded the UIView, but when i executed the code, it was not working.
But just as i declared them as #property in my .h file, the program was working completely fine.
What is the reason behind that?
What is the difference between declaring it as #property and without declaring it as #property?
Here are my files.
Before using #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
UIDynamicAnimator* myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior* gravityBehaviour = [[UIGravityBehavior alloc] initWithItems:#[box]];
[myAnimator addBehavior: gravityBehaviour];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
After Declaring it as #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong,nonatomic) UIDynamicAnimator* myAnimator ;
#property (strong,nonatomic) UIGravityBehavior *gravityBehavior;
#property (strong,nonatomic) UICollisionBehavior *collisionBehavior ;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
self.myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[box]];
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[box]];
self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.myAnimator addBehavior:self.gravityBehavior];
[self.myAnimator addBehavior:self.collisionBehavior];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You don't have to move UIGravityBehavior to a property, only UIDynamicAnimator to make it work. However, it's probably a good idea anyway in case you want to remove specific gravity behaviours rather than all of them at once.
If you are using ARC:
The UIDynamicAnimator probably does not have a strong reference to the UIGravityBehavior. So after your viewDidLoad method returns, no one owns that UIGravityBehavior and ARC will make sure that that point it is released. When you make a property for it, your viewController owns it, so the UIGravityBehavior will survive until you viewController gets deallocated.

Property 'delegate' not found on object

So here's my WSWordlistTableView.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface WSWordlistTableView : UITableView {
}
#property (nonatomic, assign) id <UITableViewDataSource> datasource;
#property (nonatomic, assign) id <UITableViewDelegate> delegate;
- (id)initWithFrame:(CGRect)frame;
#end
As you can see, there are delegate and datasource properties.
My WSWordlistTableViewController.h:
#import <UIKit/UIKit.h>
#import "WSWordlistManager.h"
#import "WSUserManager.h"
#interface WSWordlistTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#end
So, I'm building a sliding drawer view with this table that slides over the main VC. Here is the method in the main VC that creates the view and controller and then slides them onto screen:
- (IBAction)slideWordlistView:(id)sender {
if(!self.wordlistTableView ) {
CGRect frame = CGRectMake(30, 340, 100, 310);
self.wordlistTableView = [[WSWordlistTableView alloc] initWithFrame:frame];
}
[self.view bringSubviewToFront:self.wordlistTableView];
if(!self.wordlistTableViewController) {
self.wordlistTableViewController = [[WSWordlistTableViewController alloc] init];
}
self.wordlistTableViewController.view.frame = self.wordlistTableView.bounds;
self.wordlistTableViewController.view.autoresizingMask = self.wordlistTableView.autoresizingMask;
[self addChildViewController:self.wordlistTableViewController];
[self.wordlistTableView addSubview:self.wordlistTableViewController.view];
[self.wordlistTableViewController didMoveToParentViewController:self];
// *** THESE LINES THROW THE ERRORS
self.wordlistTableView.delegate = self.wordlistTableViewController;
self.wordlistTableView.datasource = self.wordlistTableViewController;
CGFloat newX = kVisibleX;
NSLog(#"newX = %f", newX);
[UIView animateWithDuration: .25
animations:
^{
CGRect drawerFrame = self.wordlistTableView.frame;
drawerFrame.origin.x = newX;
self.wordlistTableView.frame = drawerFrame;
}];
}
self.wordlistTableView.delegate = self.wordlistTableViewController;
self.wordlistTableView.datasource = self.wordlistTableViewController;
Those two lines both throw errors saying they can't find the properties 'delegate' nor 'datasource' on 'WSWordlistTableView*'. They're both there. I don't get it.

MGSplitViewController not as RootView but within a UIViewController

I'm very new to iOS programming (Coming from Java / C++). I'm trying to set up an app with a TabBarController of which one tab should be a SplitView. I've done my research and I know that UISplitview will not work and everywhere people recommend using the MGSplitViewController. I've looked at the demo but I just can't figure out how to use it without it beeing the app's root view and can't find any sample code that could help
So here is what I do with the classes from the demo in a separate UIViewController class that I afterwards add to the TabBarController: This is my class:
#import <UIKit/UIKit.h>
#import "MGSplitCornersView.h"
#import "RootViewController.h"
#import "DetailViewController.h"
#interface ChannelViewController : UIViewController {
MGSplitViewController *splitViewController;
RootViewController *rootViewController;
DetailViewController *detailViewController;
}
#property (nonatomic, retain) MGSplitViewController *splitViewController;
#property (nonatomic, retain) RootViewController *rootViewController;
#property (nonatomic, retain) DetailViewController *detailViewController;
#end
And this is my desperate try to set it up
- (id)initWithTabBar
{
self = [super init];
//this is the label on the tab button itself
self.title = #"SplitView";
//use whatever image you want and add it to your project
//self.tabBarItem.image = [UIImage imageNamed:#"name_gray.png"];
// set the long name shown in the navigation bar at the top
self.navigationItem.title=#"Nav Title";
self.splitViewController = [[MGSplitViewController alloc] init];
self.rootViewController = [[RootViewController alloc] init];
self.detailViewController = [[DetailViewController alloc] init];
[self.splitViewController setDetailViewController:detailViewController];
[self.splitViewController setMasterViewController:rootViewController];
[self.view addSubview:splitViewController.view];
[self.rootViewController performSelector:#selector(selectFirstRow) withObject:nil afterDelay:0];
[self.detailViewController performSelector:#selector(configureView) withObject:nil afterDelay:0];
if (NO) { // whether to allow dragging the divider to move the split.
splitViewController.splitWidth = 15.0; // make it wide enough to actually drag!
splitViewController.allowsDraggingDivider = YES;
}
return self;
}
I guess I'm doing something wrong with delegates? Or do I have something else mixed up?
Is the demo doing things in the IB that I can't see in the code?
I get the split view but no content and especially no navigation bar with the buttons the demo comes with.
I'd be very thankful for hints or sample code!
Ok manny, here we go. This is my working code for the interface:
#import <UIKit/UIKit.h>
#import "MGSplitViewController.h"
#import "ecbView.h"
#import "ecbCalc.h"
#interface splitMain : MGSplitViewController <UIPopoverControllerDelegate,
MGSplitViewControllerDelegate>
{
IBOutlet UIPopoverController* popoverController;
IBOutlet UINavigationController* naviController;
IBOutlet ecbCalc* viewCalcLeft;
IBOutlet ecbView* euroRatesRight;
UIBarButtonItem* savedButtonItem;
BOOL keepMasterInPortraitMode;
BOOL memoryWasDropped;
BOOL viewLoaded;
}
#property (nonatomic, retain) UIPopoverController* popoverController;
#property (nonatomic, retain) UINavigationController* naviController;
#property (nonatomic, retain) ecbCalc* viewCalcLeft;
#property (nonatomic, retain) ecbView* euroRatesRight;
#property (nonatomic, retain) UIBarButtonItem* savedButtonItem;
#property (nonatomic, readonly) BOOL keepMasterInPortraitMode;
#property (nonatomic, readonly) BOOL memoryWasDropped;
#property (nonatomic, readonly) BOOL viewLoaded;
- (void)dismissPopoverController: (BOOL)animated;
- (void)settingsChanged;
#end
and here excerpts from implementation file:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
// my initialization...
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
CGRect rectFrame = CGRectMake(0.0, 20.0, 768.0, 1004.0 - 48.0); // being above a tab bar!
viewLoaded = NO;
self.view = [[UIView alloc] initWithFrame:rectFrame];
viewCalcLeft = [[ecbCalc alloc] initWithNibName:#"ecbCalc" bundle:nil];
euroRatesRight = [[ecbView alloc] initWithNibName:#"ecbView-iPad" bundle:nil];
naviController = [[UINavigationController alloc] initWithRootViewController:self.viewCalcLeft];
naviController.navigationBar.barStyle = UIBarStyleBlack;
naviController.title = nil;
viewCalcLeft.title = NSLocalizedString(#"BtnTitleCalc", #"");
viewCalcLeft.view.hidden = NO;
NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:#"iPadAlwaysSplitTableView"] != nil)
self.keepMasterInPortraitMode = [prefs boolForKey:#"iPadAlwaysSplitTableView"];
else
self.keepMasterInPortraitMode = YES;
NSArray* theViewControllers = [NSArray arrayWithObjects:self.naviController, self.euroRatesRight, nil];
[self setViewControllers:theViewControllers];
[self setDelegate:self];
[self setShowsMasterInPortrait:keepMasterInPortraitMode];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// protection because this one is called twice
if (viewLoaded)
return;
[super viewDidLoad];
if (memoryWasDropped)
{
if (!self.keepMasterInPortraitMode && UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
// recreate popover controller
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:self.viewCalcLeft];
}
}
viewLoaded = YES;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
memoryWasDropped = YES;
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self dismissPopoverController:NO];
self.popoverController = nil;
self.naviController = nil;
self.viewCalcLeft = nil;
self.euroRatesRight = nil;
viewLoaded = NO;
}
My MainWindow.xib has a UITabBarController and the button for splitMain is configured for this class but with an empty xib entry. So creation has to go via loadView. Maybe I could have done the viewDidLoad stuff within loadView ... but so I had to protect viewDidLoad from being called twice. That happens in loadView as soon as the view is instantiated from MGSplitViewController class because the initWithCoder there is calling [self setup]. In that function the frame rect is calculated with self.view.bounds so that viewDidLoad is called again because the view doesn't exist yet. Maybe one could implement a workaround within MGSplitViewController.m but I was too lazy doing that.
To get this working on a tab bar controller please make sure you commit most of the changes that are published on the MGSplitViewController's git page. Good luck.

Resources