Unexpected '#' in xcode after upgrade of xcode - ios6

Can someone help me out with why this error showed up after I upgraded to the newest version of xcode? The app was working fine prior to this, but this syntax error prevents compiling now. I am getting the error at
#pragma mark -#property (nonatomic, k Constants,
and also I get a missing #end error.
Sorry for posting the entire page, but I wanted to make sure I provided enough to get assistance.
#import "MBProgressHUD.h"
#interface MBProgressHUD ()
(void)hideUsingAnimation:(BOOL)animated;
(void)showUsingAnimation:(BOOL)animated;
(void)fillRoundedRect:(CGRect)rect inContext:(CGContextRef)context;
(void)done;
(void)updateLabelText:(NSString *)newText;
(void)updateDetailsLabelText:(NSString *)newText;
(void)updateProgress;
(void)updateIndicators;
(void)handleGraceTimer:(NSTimer *)theTimer;
(void)handleMinShowTimer:(NSTimer *)theTimer;
#property (retain) UIView *indicator;
#property (assign) float width;
#property (assign) float height;
#property (retain) NSTimer *graceTimer;
#property (retain) NSTimer *minShowTimer;
#property (retain) NSDate *showStarted;
#end
#implementation MBProgressHUD
#pragma mark -
#pragma mark Accessors
#synthesize mode;
#synthesize delegate;
#synthesize labelText;
#synthesize detailsLabelText;
#synthesize opacity;
#synthesize labelFont;
#synthesize detailsLabelFont;
#synthesize progress;
#synthesize indicator;
#synthesize width;
#synthesize height;
#synthesize xOffset;
#synthesize yOffset;
#synthesize graceTime;
#synthesize minShowTime;
#synthesize graceTimer;
#synthesize minShowTimer;
#synthesize taskInProgress;
#synthesize showStarted;
(void)setMode:(MBProgressHUDMode)newMode {
if (mode && (mode == newMode)) {
return;
}
mode = newMode;
[self performSelectorOnMainThread:#selector(updateIndicators) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsLayout) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
(void)setLabelText:(NSString *)newText {
[self performSelectorOnMainThread:#selector(updateLabelText:) withObject:newText waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsLayout) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
(void)setDetailsLabelText:(NSString *)newText {
[self performSelectorOnMainThread:#selector(updateDetailsLabelText:) withObject:newText waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsLayout) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
(void)setProgress:(float)newProgress {
progress = newProgress;
if (mode == MBProgressHUDModeDeterminate) {
[self performSelectorOnMainThread:#selector(updateProgress) withObject:nil waitUntilDone:NO];
[self performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
}
#pragma mark -
#pragma mark Accessor helpers
(void)updateLabelText:(NSString *)newText {
if (labelText != newText) {
[labelText release];
labelText = [newText copy];
}
}
(void)updateDetailsLabelText:(NSString *)newText {
if (detailsLabelText != newText) {
[detailsLabelText release];
detailsLabelText = [newText copy];
}
}
(void)updateProgress {
[(MBRoundProgressView *)indicator setProgress:progress];
}
(void)updateIndicators {
if (indicator) {
[indicator removeFromSuperview];
}
self.indicator = nil;
if (mode == MBProgressHUDModeDeterminate) {
self.indicator = [[MBRoundProgressView alloc] initWithDefaultSize];
}
else {
self.indicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[(UIActivityIndicatorView *)indicator startAnimating];
}
[self addSubview:indicator];
}
**#pragma mark -
#property (nonatomic, k Constants**
#define MARGIN 20.0
#define PADDING 4.0
#define LABELFONTSIZE 22.0
#define LABELDETAILSFONTSIZE 16.0
#define PI 3.14159265358979323846
#pragma mark -
#pragma mark Lifecycle methods
(id)initWithWindow:(UIWindow *)window {
#property (nonatomic, nonatomic, nonatomic, [self initWithView:window];
}
(id)initWithView:(UIView *)view {
initializer above)
if (!view) {
#property (nonatomic, ion raise:#"MBProgressHUDViewIsNillException"
format:#"The view used in the MBProgressHUD initializer is nil."];
}
return [self initWithFrame:view.bounds];
}
(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.mode = MBProgressHUDModeIndeterminate;
self.labelText = nil;
self.detailsLabelText = nil;
self.opacity = 0.9;
self.labelFont = [UIFont boldSystemFontOfSize:LABELFONTSIZE];
self.detailsLabelFont = [UIFont boldSystemFontOfSize:LABELDETAILSFONTSIZE];
self.xOffset = 0.0;
self.yOffset = 0.0;
self.graceTime = 0.0;
self.minShowTime = 0.0;
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.0;
label = [[UILabel alloc] initWithFrame:self.bounds];
detailsLabel = [[UILabel alloc] initWithFrame:self.bounds];
taskInProgress = NO;
}
return self;
}
(void)dealloc {
[indicator release];
[label release];
[detailsLabel release];
[labelText release];
[detailsLabelText release];
[graceTimer release];
[minShowTimer release];
[showStarted release];
[super dealloc];
}
#pragma mark -
#pragma mark Layout
(void)layoutSubviews {
CGRect frame = self.bounds;
#property (nonatomic, indFrame = indicator.bounds;
self.width = indFrame.size.width + 2 * MARGIN;
self.height = indFrame.size.height + 2 * MARGIN;
indFrame.origin.x = floor((frame.size.width - indFrame.size.width) / 2) + self.xOffset;
indFrame.origin.y = floor((frame.size.height - indFrame.size.height) / 2) + self.yOffset;
indicator.frame = indFrame;
if (nil != self.labelText) {
CGSize dims = [self.labelText sizeWithFont:self.labelFont];
clip the label width
float lHeight = dims.height;
float lWidth;
if (dims.width <= (frame.size.width - 2 * MARGIN)) {
lWidth = dims.width;
}
else {
lWidth = frame.size.width - 4 * MARGIN;
}
label.font = self.labelFont;
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentCenter;
label.opaque = NO;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = self.labelText;
if (self.width < (lWidth + 2 * MARGIN)) {
self.width = lWidth + 2 * MARGIN;
}
self.height = self.height + lHeight + PADDING;
indFrame.origin.y -= (floor(lHeight / 2 + PADDING / 2));
indicator.frame = indFrame;
// Set the label position and dimensions
CGRect lFrame = CGRectMake(floor((frame.size.width - lWidth) / 2) + xOffset,
floor(indFrame.origin.y + indFrame.size.height + PADDING),
lWidth, lHeight);
label.frame = lFrame;
[self addSubview:label];
// Add details label delatils text was set
if (nil != self.detailsLabelText) {
// Get size of label text
dims = [self.detailsLabelText sizeWithFont:self.detailsLabelFont];
// Compute label dimensions based on font metrics if size is larger than max then clip the label width
lHeight = dims.height;
if (dims.width <= (frame.size.width - 2 * MARGIN)) {
lWidth = dims.width;
}
else {
lWidth = frame.size.width - 4 * MARGIN;
}
// Set label properties
detailsLabel.font = self.detailsLabelFont;
detailsLabel.adjustsFontSizeToFitWidth = NO;
detailsLabel.textAlignment = UITextAlignmentCenter;
detailsLabel.opaque = NO;
detailsLabel.backgroundColor = [UIColor clearColor];
detailsLabel.textColor = [UIColor whiteColor];
detailsLabel.text = self.detailsLabelText;
// Update HUD size
if (self.width < lWidth) {
self.width = lWidth + 2 * MARGIN;
}
self.height = self.height + lHeight + PADDING;
// Move indicator to make room for the new label
indFrame.origin.y -= (floor(lHeight / 2 + PADDING / 2));
indicator.frame = indFrame;
// Move first label to make room for the new label
lFrame.origin.y -= (floor(lHeight / 2 + PADDING / 2));
label.frame = lFrame;
// Set label position and dimensions
CGRect lFrameD = CGRectMake(floor((frame.size.width - lWidth) / 2) + xOffset,
lFrame.origin.y + lFrame.size.height + PADDING, lWidth, lHeight);
detailsLabel.frame = lFrameD;
[self addSubview:detailsLabel];
}
}
}
#pragma mark -
#pragma mark Showing and execution
- (void)show:(BOOL)animated {
useAnimation = animated;
// If the grace time is set postpone the HUD display
if (self.graceTime > 0.0) {
self.graceTimer = [NSTimer scheduledTimerWithTimeInterval:self.graceTime
target:self
selector:#selector(handleGraceTimer:)
userInfo:nil
repeats:NO];
}
// ... otherwise show the HUD imediately
else {
[self setNeedsDisplay];
[self showUsingAnimation:useAnimation];
}
}
- (void)hide:(BOOL)animated {
useAnimation = animated;
// If the minShow time is set, calculate how long the hud was shown,
// and pospone the hiding operation if necessary
if (self.minShowTime > 0.0 && showStarted) {
NSTimeInterval interv = [[NSDate date] timeIntervalSinceDate:showStarted];
if (interv < self.minShowTime) {
self.minShowTimer = [NSTimer scheduledTimerWithTimeInterval:(self.minShowTime - interv)
target:self
selector:#selector(handleMinShowTimer:)
userInfo:nil
repeats:NO];
return;
}
}
// ... otherwise hide the HUD immediately
[self hideUsingAnimation:useAnimation];
}
- (void)handleGraceTimer:(NSTimer *)theTimer {
// Show the HUD only if the task is still running
if (taskInProgress) {
[self setNeedsDisplay];
[self showUsingAnimation:useAnimation];
}
}
- (void)handleMinShowTimer:(NSTimer *)theTimer {
[self hideUsingAnimation:useAnimation];
}
- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {
methodForExecution = method;
targetForExecution = [target retain];
objectForExecution = [object retain];
// Launch execution in new thread
taskInProgress = YES;
[NSThread detachNewThreadSelector:#selector(launchExecution) toTarget:self withObject:nil];
// Show HUD view
[self show:animated];
}
- (void)launchExecution {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Start executing the requested task
[targetForExecution performSelector:methodForExecution withObject:objectForExecution];
// Task completed, update view in main thread (note: view operations should
// be done only in the main thread)
[self performSelectorOnMainThread:#selector(cleanUp) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void*)context {
[self done];
}
- (void)done {
isFinished = YES;
// If delegate was set make the callback
self.alpha = 0.0;
if(delegate != nil && [delegate conformsToProtocol:#protocol(MBProgressHUDDelegate)]) {
if([delegate respondsToSelector:#selector(hudWasHidden)]) {
[delegate performSelector:#selector(hudWasHidden)];
}
}
}
- (void)cleanUp {
taskInProgress = NO;
self.indicator = nil;
[targetForExecution release];
[objectForExecution release];
[self hide:useAnimation];
}
#pragma mark -
#pragma mark Fade in and Fade out
- (void)showUsingAnimation:(BOOL)animated {
self.showStarted = [NSDate date];
// Fade in
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.40];
self.alpha = 1.0;
[UIView commitAnimations];
}
else {
self.alpha = 1.0;
}
}
- (void)hideUsingAnimation:(BOOL)animated {
// Fade out
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationFinished: finished: context:)];
// 0.02 prevents the hud from passing through touches during the animation the hud will get completely hidden
// in the done method
self.alpha = 0.02;
[UIView commitAnimations];
}
else {
self.alpha = 0.0;
[self done];
}
}
#pragma mark BG Drawing
- (void)drawRect:(CGRect)rect {
// Center HUD
CGRect allRect = self.bounds;
// Draw rounded HUD bacgroud rect
CGRect boxRect = CGRectMake(((allRect.size.width - self.width) / 2) + self.xOffset,
((allRect.size.height - self.height) / 2) + self.yOffset, self.width, self.height);
CGContextRef ctxt = UIGraphicsGetCurrentContext();
[self fillRoundedRect:boxRect inContext:ctxt];
}
- (void)fillRoundedRect:(CGRect)rect inContext:(CGContextRef)context {
float radius = 10.0f;
CGContextBeginPath(context);
CGContextSetGrayFillColor(context, 0.0, self.opacity);
CGContextMoveToPoint(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect) + radius, radius, 3 * M_PI / 2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) + radius, radius, M_PI, 3 * M_PI / 2, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
#end
#implementation MBRoundProgressView
- (id)initWithDefaultSize {
return [super initWithFrame:CGRectMake(0.0f, 0.0f, 37.0f, 37.0f)];
}
- (void)drawRect:(CGRect)rect {
CGRect allRect = self.bounds;
CGRect circleRect = CGRectMake(allRect.origin.x + 2, allRect.origin.y + 2, allRect.size.width - 4,
allRect.size.height - 4);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw background
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.1); // translucent white
CGContextSetLineWidth(context, 2.0);
CGContextFillEllipseInRect(context, circleRect);
CGContextStrokeEllipseInRect(context, circleRect);
// Draw progress
float x = (allRect.size.width / 2);
float y = (allRect.size.height / 2);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); // white
CGContextMoveToPoint(context, x, y);
CGContextAddArc(context, x, y, (allRect.size.width - 4) / 2, -(PI / 2), (self.progress * 2 * PI) - PI / 2, 0);
CGContextClosePath(context);
CGContextFillPath(context);
}
#end

The line throwing the error makes no sense.
#pragma mark -
#property (nonatomic, k Constants
the #pragma mark - puts a line in the drop down list but what is the next line supposed to do?
if you are declaring a property, then you haven't closed the bracket after nonatomic, but when what is k Constants supposed to be. Are you sure you didn't change the file?

Related

SceneKit: how to create chessboard pattern for SCNFloor from image?

The goal is to create an infinite chessboard pattern.
Using a SCNFloor and the attached image, we produce something close but not quite like a chessboard. Some black squares merge where they shouldn't.
We tried different values for Scale, WrapS, WrapT, Min filter, Map filter, and Mip filter. The screenshot shows the current values.
Is the underlying image not correct, or what setting do we need to change for the SCNFloor?
Repeated image:
Result:
#import "GameViewController.h"
#interface GameViewController ()
#property (nonatomic) CGFloat chessBoardWidth;
#property (nonatomic) CGFloat chessBoardDepth;
#property (nonatomic) CGFloat tileWidth;
#property (nonatomic) CGFloat tileDepth;
#property (nonatomic, getter=isOdd) BOOL odd;
#end
#implementation GameViewController
-(instancetype)init {
self = [super init];
if(self) {
self.chessBoardWidth = 10.0f;
self.chessBoardDepth = 10.0f;
self.tileWidth = 1.0f;
self.tileDepth = 1.0f;
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
// create a new scene
SCNScene *scene = [SCNScene sceneNamed:#"art.scnassets/chessboard.scn"];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 150);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient;
ambientLightNode.light.color = [NSColor darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
// Material
SCNMaterial *blackMaterial = [SCNMaterial material];
blackMaterial.diffuse.contents = [NSColor blackColor];
SCNMaterial *whiteMaterial = [SCNMaterial material];
whiteMaterial.diffuse.contents = [NSColor whiteColor];
// Geometry
SCNPlane *blackTile = [[SCNPlane alloc] init];
blackTile.firstMaterial = blackMaterial;
SCNPlane *whiteTile = [[SCNPlane alloc] init];
whiteTile.firstMaterial = whiteMaterial;
// Parent node
SCNNode *parentNode = [[SCNNode alloc] init];
[scene.rootNode addChildNode:parentNode];
self.odd = YES;
for (uint x=0; x < self.chessBoardWidth; x++) {
for (uint z=0; z < self.chessBoardDepth; z++) {
// Add tile
SCNNode *tileNode = [[SCNNode alloc] init];
if(self.isOdd) {
tileNode.geometry = blackTile;
} else {
tileNode.geometry = whiteTile;
}
[parentNode addChildNode:tileNode];
// Position tile
tileNode.position = SCNVector3Make(self.tileWidth * x, 0, self.tileDepth * z);
// Alternate
if(self.isOdd) {
self.odd = NO;
} else {
self.odd = YES;
}
}
}
// set the scene to the view
self.gameView.scene = scene;
// allows the user to manipulate the camera
self.gameView.allowsCameraControl = YES;
// show statistics such as fps and timing information
self.gameView.showsStatistics = YES;
// configure the view
self.gameView.backgroundColor = [NSColor grayColor];
}
#end

CorePlot refresh plot connected to table

I'm working with core plot on xCode and I want to plot some real-time incoming data via bluetooth.
I used the CPTTestAPP for Mac Os X as example and tried to adapt it to my specific case.
The first 60 points are added like it's supposed to and due the following unaltered code:
-(void)setupScatterPlots
{
static BOOL hasData = NO;
// Create one plot that uses bindings
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = bindingsPlot;
CPTMutableLineStyle *lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
[self.graph addPlot:boundLinePlot];
[boundLinePlot bind:CPTScatterPlotBindingXValues toObject:self withKeyPath:#"arrangedObjects.x" options:nil];
[boundLinePlot bind:CPTScatterPlotBindingYValues toObject:self withKeyPath:#"arrangedObjects.y" options:nil];
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
boundLinePlot.delegate = self;
boundLinePlot.plotSymbolMarginForHitDetection = 5.0;
if ( !hasData ) {
// Add some initial data
contentArray = [NSMutableArray arrayWithCapacity:100];
for ( NSUInteger i = 0; i < 60; i++ ) {
NSNumber *x = #(1.0 + i * 0.05);
NSNumber *y = #(1.2 * arc4random() / (double)UINT32_MAX + 1.2);
[contentArray addObject:#{ #"x": x,
#"y": y }
];
}
self.content = contentArray;
hasData = YES;
}
// Auto scale the plot space to fit the plot data
// Extend the y range by 10% for neatness
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsMomentum = YES;
[plotSpace scaleToFitPlots:#[boundLinePlot]];
CPTPlotRange *xRange = plotSpace.xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.1)];
plotSpace.yRange = yRange;
// Restrict y range to a global range
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(10.0)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(5.0)];
// set the x and y shift to match the new ranges
CGFloat length = xRange.lengthDouble;
self.xShift = length - 3.0;
length = yRange.lengthDouble;
self.yShift = length - 2.0;
}
I'm using a method called insertData who looks like this:
-(void)insertData:(uint32 *) buf :(uint32 *) i
{
[contentArray addObject:#{ #"x": i,
#"y": buf }
];
NSLog(#"%#",contentArray);
self.content = contentArray;
}
where buf is my incoming data.
But when I call the insertData method, the data are appended to the array but the tab on my interface doesn't change, neither does the plot.
can anyone help me please?
thanks !
I already tried to convert the int to NSNumber by using the following :
NSNumber *ni = #(*i);
NSNumber *nbuf = #(*buf);
and:
NSNumber *ni = [NSNumber numberWithUnsignedInt:(*i);
NSNumber *nbuf = [NSNumber numberWithUnsignedInt:(*buf);
I also tried all the reload stuff I found over the internet
but none of that worked either.
#import "Controller.h"
#import "HeartRateMonitorAppDelegate.h"
static const CGFloat kZDistanceBetweenLayers = 20.0;
static NSString *const bindingsPlot = #"Bindings Plot";
static NSMutableArray *contentArray;
#interface Controller()
#property (nonatomic, readwrite, strong) IBOutlet CPTGraphHostingView *hostView;
#property (nonatomic, readwrite, strong) CPTXYGraph *graph;
#property (nonatomic, readwrite, strong) CPTPlotSpaceAnnotation *symbolTextAnnotation;
-(void)setupGraph;
-(void)setupAxes;
-(void)setupScatterPlots;
#end
#pragma mark -
#implementation Controller
#synthesize hostView;
#synthesize graph;
#synthesize symbolTextAnnotation;
+(void)initialize
{
[NSValueTransformer setValueTransformer:[CPTDecimalNumberValueTransformer new] forName:#"CPTDecimalNumberValueTransformer"];
}
-(void)awakeFromNib
{
[super awakeFromNib];
self.xShift = 0.0;
self.yShift = 0.0;
[self setupGraph];
[self setupAxes];
[self setupScatterPlots];
}
-(id)newObject:(uint32 *) buf :(uint32 *) i
{
NSLog(#"buf = %d", *buf);
NSNumber *x1 = #(*i);
NSNumber *y1 = #(*i);
return #{
#"x": x1,
#"y": y1
};
}
#pragma mark -
#pragma mark Graph Setup Methods
-(void)setupGraph
{
// Create graph and apply a dark theme
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:NSRectToCGRect(self.hostView.bounds)];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[newGraph applyTheme:theme];
self.hostView.hostedGraph = newGraph;
self.graph = newGraph;
// Graph padding
newGraph.paddingLeft = 0.0;
newGraph.paddingTop = 0.0;
newGraph.paddingRight = 0.0;
newGraph.paddingBottom = 0.0;
// Plot area delegate
newGraph.plotAreaFrame.plotArea.delegate = self;
}
-(void)setupAxes
{
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle];
redLineStyle.lineWidth = 10.0;
redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5];
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromDouble(0.5);
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
x.minorTicksPerInterval = 2;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
NSArray *exclusionRanges = #[[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(2.99) length:CPTDecimalFromDouble(0.02)]];
x.labelExclusionRanges = exclusionRanges;
// Label y with an automatic label policy.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
y.minorTicksPerInterval = 2;
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.labelOffset = 10.0;
exclusionRanges = #[[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.99) length:CPTDecimalFromDouble(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(3.99) length:CPTDecimalFromDouble(0.02)]];
y.labelExclusionRanges = exclusionRanges;
}
-(void)setupScatterPlots
{
static BOOL hasData = NO;
// Create one plot that uses bindings
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = bindingsPlot;
CPTMutableLineStyle *lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 2.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
[self.graph addPlot:boundLinePlot];
[boundLinePlot bind:CPTScatterPlotBindingXValues toObject:self withKeyPath:#"arrangedObjects.x" options:nil];
[boundLinePlot bind:CPTScatterPlotBindingYValues toObject:self withKeyPath:#"arrangedObjects.y" options:nil];
// Set plot delegate, to know when symbols have been touched
// We will display an annotation when a symbol is touched
boundLinePlot.delegate = self;
boundLinePlot.plotSymbolMarginForHitDetection = 5.0;
if ( !hasData ) {
// Add some initial data
contentArray = [NSMutableArray arrayWithCapacity:100];
for ( NSUInteger i = 0; i < 5; i++ ) {
NSNumber *x = #(1.0 + i * 0.05);
NSNumber *y = #(1.2 * arc4random() / (double)UINT32_MAX + 1.2);
[contentArray addObject:#{ #"x": x,
#"y": y }
];
}
self.content = contentArray;
hasData = YES;
}
// Auto scale the plot space to fit the plot data
// Extend the y range by 10% for neatness
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsMomentum = YES;
[plotSpace scaleToFitPlots:#[boundLinePlot]];
CPTPlotRange *xRange = plotSpace.xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.1)];
plotSpace.yRange = yRange;
// Restrict y range to a global range
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(10.0)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(5.0)];
// set the x and y shift to match the new ranges
CGFloat length = xRange.lengthDouble;
self.xShift = length - 3.0;
length = yRange.lengthDouble;
self.yShift = length - 2.0;
}
-(void)insertData:(uint32 *) buf :(uint32 *) i
{
NSNumber *ni = #(*i);
//NSNumber *nbuf = #(*buf);
[contentArray addObject:#{ #"x": ni,
#"y": ni }
];
self.content = contentArray;
[graph reloadData];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
return 8;
}
else {
return [self.arrangedObjects count];
}
}
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num;
if ( [plot isKindOfClass:[CPTBarPlot class]] ) {
num = #( (index + 1) * (index + 1) );
if ( [plot.identifier isEqual:bindingsPlot] ) {
num = #(num.integerValue - 10);
}
}
else {
NSString *key = (fieldEnum == CPTScatterPlotFieldX ? #"x" : #"y");
num = (self.arrangedObjects)[index][key];
if ( fieldEnum == CPTScatterPlotFieldY ) {
num = #([num doubleValue] + 1.0);
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
if ( [(NSString *)plot.identifier isEqualToString : bindingsPlot] ) {
return (id)[NSNull null]; // Don't show any label
}
else if ( [(NSString *)plot.identifier isEqualToString : bindingsPlot] && (index < 4) ) {
return (id)[NSNull null];
}
else if ( index % 4 ) {
return (id)[NSNull null];
}
else {
return nil; // Use default label style
}
}
#pragma mark -
#pragma mark CPTScatterPlot delegate method
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
CPTPlotSpaceAnnotation *annotation = self.symbolTextAnnotation;
if ( annotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.symbolTextAnnotation = nil;
}
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0f;
hitAnnotationTextStyle.fontName = #"Helvetica-Bold";
// Determine point of symbol in plot coordinates
NSDictionary *dataPoint = (self.arrangedObjects)[index];
NSNumber *x = dataPoint[#"x"];
NSNumber *y = dataPoint[#"y"];
NSArray *anchorPoint = #[x, y];
// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:y];
// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle];
annotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
annotation.contentLayer = textLayer;
annotation.displacement = CGPointMake(0.0, 20.0);
[self.graph.plotAreaFrame.plotArea addAnnotation:annotation];
self.symbolTextAnnotation = annotation;
}
#pragma mark -
#pragma mark Plot area delegate method
-(void)plotAreaWasSelected:(CPTPlotArea *)plotArea
{
// Remove the annotation
CPTPlotSpaceAnnotation *annotation = self.symbolTextAnnotation;
if ( annotation ) {
[self.graph.plotAreaFrame.plotArea removeAnnotation:annotation];
self.symbolTextAnnotation = nil;
}
}
#pragma mark -
#pragma mark PDF / image export
-(IBAction)exportToPDF:(id)sender
{
NSSavePanel *pdfSavingDialog = [NSSavePanel savePanel];
[pdfSavingDialog setAllowedFileTypes:#[#"pdf"]];
if ( [pdfSavingDialog runModal] == NSOKButton ) {
NSData *dataForPDF = [self.graph dataForPDFRepresentationOfLayer];
[dataForPDF writeToURL:[pdfSavingDialog URL] atomically:NO];
}
}
-(IBAction)exportToPNG:(id)sender
{
NSSavePanel *pngSavingDialog = [NSSavePanel savePanel];
[pngSavingDialog setAllowedFileTypes:#[#"png"]];
if ( [pngSavingDialog runModal] == NSOKButton ) {
NSImage *image = [self.graph imageOfLayer];
NSData *tiffData = [image TIFFRepresentation];
NSBitmapImageRep *tiffRep = [NSBitmapImageRep imageRepWithData:tiffData];
NSData *pngData = [tiffRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToURL:[pngSavingDialog URL] atomically:NO];
}
}
#pragma mark -
#pragma mark Printing
-(IBAction)printDocument:(id)sender
{
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
NSRect printRect = NSZeroRect;
printRect.size.width = (printInfo.paperSize.width - printInfo.leftMargin - printInfo.rightMargin) * printInfo.scalingFactor;
printRect.size.height = (printInfo.paperSize.height - printInfo.topMargin - printInfo.bottomMargin) * printInfo.scalingFactor;
self.hostView.printRect = printRect;
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:self.hostView printInfo:printInfo];
[printOperation runOperationModalForWindow:self.hostView.window
delegate:self
didRunSelector:#selector(printOperationDidRun:success:contextInfo:)
contextInfo:NULL];
}
-(void)printOperationDidRun:(NSPrintOperation *)printOperation success:(BOOL)success contextInfo:(void *)contextInfo
{
// print delegate
}
Core Plot expects the data values to be NSNumber instances, not plain integers. You can use NSDecimalNumber (a subclass of NSNumber) if you need to preserve full decimal precision.
Since you're using data bound to an NSArrayController, -reloadData and related methods will have no effect. Those are only used when providing data through a datasource. That's the only method available on iOS which is why much of the sample code and demos on the web use it.
Once an NSArrayController is initialized, you should use its methods to add or remove data rather than updating the content array directly. The CPTTestAPP demo uses -insertObject:atArrangedObjectIndex: and -removeObjectAtArrangedObjectIndex: to do this.

CATransform3D not being applied properly in custom UICollectionViewFlowLayout

I'm having a strange issue with custom UICollectionViewFlowLayout, my cells contain an imageView, I apply a zoom effect to my cells... however I think that I'm doing something wrong with the Zoom Effect, it's strange, because my cells get scaled correctly, the problem is that the imageView for some cells is not.
Here's the relevant code (I think) for this issue, (note that If I remove the method: - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect or the CATransform3DMakeScale, the problem doesn't seem to appear.)
///// UICollectionViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
HICollectionViewCell *cell = (HICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:COLLECTION_VIEW_CELL_ID forIndexPath:indexPath];
[self customizeCell:cell atIndexPath:indexPath];
return cell;
}
#pragma mark - CollectionView FlowDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(200.0f, 200.0f);
}
#pragma mark - "Private" Methods
- (void)customizeCell:(HICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
UIImageView *view = (UIImageView *)[cell.contentView viewWithTag:kCollectionViewCellSubViewTag_imgView];
if (!view.image) {
view.image = [UIImage imageNamed:#"placeholder.png"];
}
}
The Custom Layout Inherits from UICollectionViewFlowLayout
/////// CustomLayout.m
static const CGFloat ACTIVE_DISTANCE = 200.0f;
static const CGFloat ZOOM_FACTOR = 0.3f;
- (id)init
{
if(self = [super init]) {
self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(200.0f, 0.0f, 200.0f, 0.0f);
self.minimumLineSpacing = 50.0f;
}
return self;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for (UICollectionViewLayoutAttributes *attribute in attributes) {
if (CGRectIntersectsRect(attribute.frame, rect)) {
CGFloat distance = CGRectGetMidX(visibleRect) - attribute.center.x;
CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
// Apply a zoom factor to cells within ACTIVE_DISTANCE
if (ABS(distance) < ACTIVE_DISTANCE) {
CGFloat zoom = 1.0f + ZOOM_FACTOR * (1.0f - ABS(normalizedDistance));
attribute.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
attribute.zIndex = round(zoom);
}
}
}
return attributes;
}
As the FlowLayout appears everything looks fine.
Then I start to scroll and I notice that the background layer of the cells to the right starts to show off (backgroundColor is orange)
As you can see, after two cells, the third one ( that starts to appear, looks OK again ).
Maybe it's an issue with the cell queue and dequeue (and the imageView being re-used or something?).

drawing rounded rect on imageContext using UIBeizerpath not working

I had a requirement to draw a rounded rect while user touch is moving on iPad.
Here is my code it is showing black color rounded rect while touch moving and selected color when touch is ended. Can any one please tell me the reason?
- (void)drawRect:(CGRect)rect
{
[path setLineWidth:lineSize];
[selectedColor setStroke];
[drawImage drawInRect:rect]; // (3)
[path fill];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
startPoint=p;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
float x=startPoint.x;
float y=startPoint.y;
float z=p.x-startPoint.x;
float a=p.y-startPoint.y;
if (z<0 && a<0) {
x=p.x;
y=p.y;
z=startPoint.x-p.x;
a=startPoint.y-p.y;
}
else if (a<0)
{
x=startPoint.x;
y=startPoint.y;
z=p.x-startPoint.x;
a=startPoint.y-p.y;
}
else if(z<0)
{
x=startPoint.x;
y=startPoint.y;
z=startPoint.x-p.x;
a=p.y-startPoint.y;
}
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y,z,a) cornerRadius:10.0];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[self drawBitmap];
[self setNeedsDisplay];
}
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[selectedColor setStroke];
if (!drawImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor whiteColor] setFill];
[rectpath fill]; // filling it with white
}
[drawImage drawAtPoint:CGPointZero];
[selectedColor setFill];
[path fill];
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Edit your initWithFrame method to make background of the UIView white.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}

how to add delay to mkannotation pin drop

In my map view I set the map to open, then with a delay of 2 seconds the map zooms in to show my mkannotation, what I have been trying to do is to animate the pin dropping once the view is fully zoomed in, but haven't been able to achieve this.
So basically I want to add a delay to the annotation+pin being dropped at my location.
How do I do this?
Code what I have in place currently in ViewDidLoad, the code for the mkannotation is in void - showDetails:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 54.5;
region.center.longitude = -3.5;
region.span.longitudeDelta = 10.0f;
region.span.latitudeDelta = 10.0f;
[mapView setRegion:region animated:NO];
[self performSelector:#selector(zoomInToMyLocation)
withObject:nil
afterDelay:2]; //will zoom in after 2 seconds
}
-(void)zoomInToMyLocation
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 51.502729 ;
region.center.longitude = -0.071948;
region.span.longitudeDelta = 0.19f;
region.span.latitudeDelta = 0.19f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #"Design Museum";
ann.subtitle = #"Camberwell, London";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
If you set the delegate before you set the region your delegate will receive a call to mapView:regionDidChangeAnimated. That is called many times while scrolling so what you could do is check the map's current center point and when it is close enough to your target center point you add the annotation.
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setDelegate:self];
............
haveAddedPin = false;
}
-(void)zoomInToMyLocation
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 51.502729 ;
region.center.longitude = -0.071948;
region.span.longitudeDelta = 0.19f;
region.span.latitudeDelta = 0.19f;
[mapView setRegion:region animated:YES];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(Boolean) animated)
{
if (haveAddedPin == false && mapView.center == the coordinates of the target you are zooming to)
{
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #"Design Museum";
ann.subtitle = #"Camberwell, London";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
haveAddedPin = true;
}
}

Resources