I created a category like so:
#import "UIViewController+Dimensions.h"
#implementation UIView (Dimensions)
- (double) screenWidth2
{
return self.frame.size.width;
}
- (double) screenHeight2
{
return self.frame.size.height;
}
#end
But when I try to call it in my view controller after importing it, I get:
"Use of undeclared identifier screenWidth2;
Is there a better way to grab the screenWidth in one place then every view controller I'm in?
I then did this in my view controller:
_registerButton.frame = CGRectMake(0.0, 0.0, [self screenWidth2] / 2.0 - 1.0, 60.0);
but the app compiles runs and crashes. And the category's .h file is:
#import <UIKit/UIKit.h>
#interface UIViewController (Dimensions)
- (double) screenWidth2;
- (double) screenHeight2;
#end
You need to add
#import "UIViewController+Dimensions.h"
In the code using the category (the file where you call [uiViewControllerInstance screenWidth2])
Have you checked that those variables are declared in the .h? So, they are public.
Also, to use the category, add this:
#import "UIViewController+Dimensions.h"
In the class that have to use the category.
Related
Hi all i have got to implement storehouse like animation in my project. I found a third party library but sorry it is in swift. Please help me with objective c code
Refference link for animation is
Animation Store house
StoreTableView.h
#import <UIKit/UIKit.h>
#protocol StoreHouseTableViewTransform
#property (nonatomic) CGFloat scaleValue;
-(void)transFormForCell:(CGFloat)scaleFactor;
#end
#interface StoreTableView : UITableView
#end
StoreTableView.m
#import "StoreTableView.h"
#implementation StoreTableView
-(void)layoutSubviews{
[super layoutSubviews];
[self transformTable];
}
- (void )transformTable{
for (NSIndexPath *indexpath in self.indexPathsForVisibleRows) {
UITableViewCell *cell = [self cellForRowAtIndexPath:indexpath];
if ([cell conformsToProtocol:#protocol(StoreHouseTableViewTransform)]) {
UITableViewCell <StoreHouseTableViewTransform> *vc = (UITableViewCell<StoreHouseTableViewTransform> *)cell;
CGFloat distCenter=[self computeDistance:indexpath];
[vc transFormForCell:[self computeScale:distCenter withMinScale:vc.scaleValue]];
}
}
}
-(CGFloat )computeDistance:(NSIndexPath *)indexpath{
CGRect rect=[self rectForRowAtIndexPath:indexpath];
CGFloat cellCenter=rect.origin.y + rect.size.height/2;
CGFloat cellContentCenter=self.contentOffset.y + self.bounds.size.height/2;
return fabs(cellCenter - cellContentCenter);
}
-(CGFloat )computeScale:(CGFloat)distanceOfCenter withMinScale:(CGFloat)scaleFactor{
return (1.0-scaleFactor) * distanceOfCenter / self.bounds.size.height;
}
#end
StoreTableViewCell.h
#import <UIKit/UIKit.h>
#import "StoreTableView.h"
#interface StoreTableViewCell : UITableViewCell<StoreHouseTableViewTransform>{
CGFloat minimumScale;
}
#property (weak, nonatomic) IBOutlet UIView *backView;
#property (weak, nonatomic) IBOutlet UIImageView *scaleImage;
#end
StoreTableViewCell.m
#import "StoreTableViewCell.h"
#implementation StoreTableViewCell
#synthesize scaleValue;
-(void )prepareForReuse{
[super prepareForReuse ];
minimumScale = 0.85;
self.backView.transform = CGAffineTransformMakeScale(minimumScale, minimumScale);
}
#pragma mark delegate
-(void)transFormForCell:(CGFloat)scaleFactor{
self.backView.transform = CGAffineTransformMakeScale(1.0 - scaleFactor, 1.0 - scaleFactor);
}
#end
This is objective-C implementation of StoreHouse Animation
Subclass your TableView with StoreTableView and Cell with StoreTableViewCell.
I think so this will help you
play with scaleFactor
I spent about 4 hours trying to enable Swift in my Xcode Objc-based project. My "myproject-Swift.h" file was created successfully, but my Xcode didn't see my Swift-classes. So I decided to create a new Xcode Objc-based project and finally I found the right answer! Hope this post will help someone :-)
Step by step swift integration for Xcode Objc-based project:
1.Create new *.swift file (in Xcode) or add it by using Finder
2.Add swift bridging empty header if Xcode have not done this before (see 4 below)
3.Implement your Swift class by using #objc attribute:
import UIKit
#objc class Hello : NSObject {
func sayHello () {
println("Hi there!")
}
}
4.Open Build Settings and check this parameters:
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
5. Import header (which is auto generated by Xcode) in your *.m file
#import "myproject-Swift.h"
6. Clean and rebuild your Xcode project
Profit
They have open sourced their animation engine. Built on top of CADisplayLink, they recommend this mainly if you're looking to do physics based animations that interact with gestures.
https://github.com/storehouse/Advance
I'm trying to use the SPUserResizableView created by Spoletto in github (https://github.com/spoletto/SPUserResizableView).
I imported the .h and .m files, created a bridge and added the delegates to my controller.
I think problem is that problem is that the library is very old and that's why I get many errors in the .h and .m files so I can't use the library.
Errors - http://postimg.org/gallery/3bq2ldo0m/
Can you help me setup the library in swift?
I also got the same error, Just add these two in SPUserResizableView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
Your problem will be resolved.
You need to set up an Objective-C bridging header and then import it in there. To do this:
Drag and drop the .h and .m SPUserResizableView files into your project.
When Xcode asks if you want to create a bridging header, select 'Yes'.
Type in the new file called 'YOUR-PROJECT-NAME-bridging-header.h' this code:
#import SPUserResizableView.h
You should now be able to use it in Swift!
I got this working with Swift 4 by moving the variable declarations from the interface to the implementation, and removing the release and dealloc code.
SPUserResizableView.h changed to
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
typedef struct SPUserResizableViewAnchorPoint {
CGFloat adjustsX;
CGFloat adjustsY;
CGFloat adjustsH;
CGFloat adjustsW;
} SPUserResizableViewAnchorPoint;
#protocol SPUserResizableViewDelegate;
#class SPGripViewBorderView;
#interface SPUserResizableView : UIView {
}
//...
//(identical from this point on)
SPUserResizableView.m changed to
//...
//(identical up to)
//CGColorSpaceRelease(baseSpace), baseSpace = NULL;
CGColorSpaceRelease(baseSpace);
baseSpace = NULL;
#implementation SPUserResizableView
SPGripViewBorderView *borderView;
UIView *contentView;
CGPoint touchStart;
CGFloat minWidth;
CGFloat minHeight;
// Used to determine which components of the bounds we'll be modifying, based upon where the user's touch started.
SPUserResizableViewAnchorPoint anchorPoint;
id <SPUserResizableViewDelegate> delegate;
//#synthesize contentView, minWidth, minHeight, preventsPositionOutsideSuperview, delegate;
//...
//(this part identical)
- (void)dealloc {
[contentView removeFromSuperview];
}
#end
I would like to define a number of variables that I will use in view controllers to set font size depending on whether the user is on an iphone or an ipad. So far I can define, say, a global float no problem. However, I am not able to include any conditional statements in my file. This works fine:
.h file:
#import <Foundation/Foundation.h>
#interface GlobalDeclarations : NSObject
extern float MyGlobalVariable;
#end
.m file:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
#end
But I cannot modify my .m file to include any if statement at all - I always get the same error:
"expected idenitifer or ')'
For example, this version of the .m file produces the above error:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
if(MyGlobalVariable > 2.0f)
{
NSLog(#"inside if statement");
}
#end
I get the error even if I just write something like if(TRUE)...
How can I conditionally define globals in an attachment like this? What is wrong with the if-statement? Thanks for any suggestions.
For variables you can only have declarations and limited initialisation expressions as the file level. If you need more complex initialisation you must put the code in a method, and Objective-C supports the method + initialize for doing class-wide setup.
Rewrite your code as:
#import "GlobalDeclarations.h"
#implementation GlobalDeclarations
float MyGlobalVariable = 0.0f;
+ initialize
{
if(MyGlobalVariable < 2.0f)
{
NSLog(#"inside if statement");
}
}
#end
The + initialize method will get called before the first use of the class.
Note: for more obscure situations there is also a + load method which gets called even earlier than + initialize, and there are attributes you can use to mark any function to be called in a similar way.
So I am trying to build an iPhone app that uses a SplitView for the main screen Similar to the app Lyft or like basically this controller https://github.com/mutualmobile/MMDrawerController.
I have set up the following storyboard (I am not using SizeClasses) :
I have subclassed the SplitViewController to try to set a max width for my MasterViewController:
// File .h
#import <UIKit/UIKit.h>
#interface LLSplitViewController : UISplitViewController <UISplitViewControllerDelegate>
#property(nonatomic, assign) CGFloat maximumPrimaryColumnWidth NS_AVAILABLE_IOS(8_0);
#end
// File .m
#implementation LLSplitViewController
-(void)viewDidLoad{
self.delegate = self;
self.preferredPrimaryColumnWidthFraction = .1;
CGRect mainScreen = [[UIScreen mainScreen] bounds];
self.maximumPrimaryColumnWidth = mainScreen.size.width - 100;
}
#end
What am I missing here?
I ended up using this framework to achieve my goal.
It is not a splitcontroller but it works similar to it and it seems to be very well maintained.
I am new to programming and is in the process of learning objective-c from Big Nerd Ranch. Using x-code, I am trying to create a class. Upon creating the class, X-Code is not recognizing it in main file. I created a new project, then created a new file ensuring the correct target was selected. When I try to type triangle, it says "Use of undeclared identifier." What am I doing wrong? Please help.
This is my header file.
#import <Foundation/Foundation.h>
#interface Triangle : NSObject
{
float lengthSideA;
float lengthSideB;
float lengthSideC;
}
#property float lengthSideA;
#property float lengthSideB;
#property float lengthSideC;
-(float) area;
-(float) perimeter;
-(float) hypothenuse;
#end
And this is my implementation file.
#import "Triangle.h"
#implementation Triangle
#synthesize lengthSideA, lengthSideC, lengthSideB;
-(float) perimeter
{
float a = [self lengthSideA];
float b = [self lengthSideB];
float c = [self lengthSideC];
return a + b + c;
}
-(float) area
{
float a = [self lengthSideA];
float b = [self lengthSideB];
return b * a / 2;
}
-(float) hypothenuse
{
float a = [self lengthSideA];
float b = [self lengthSideB];
return sqrt(a * a + b * b);
}
#end
Add #import "Triangle.h" in the class where you are using your Triangle class. You have to import any external class before you can use it.
First of all check if the file is imported. If it is imported, go to build settings and check if the header of the file is added to the compile sources. Also check if the file is added to the main bundle properly. I hope you selected the tick option which says add the reference of the file while adding the file. Hope it helps :)