glkview drawinrect delegate method called only once - iphone

I am trying to add GLKViewController to my UserInterfaceViewController.I am using the example project from raywenderlich tutorials(http://www.raywenderlich.com/5235/beginning-opengl-es-2-0-with-glkit-part-2)
Here is my code snippet
#import <UIKit/UIKit.h>
#interface UserInterfaceViewController : UIViewController
#end
#implementation UserInterfaceViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIViewController *userInterfaceController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"HelloGLKitViewController"];
userInterfaceController.view.frame = CGRectMake(50.0, 100.0, 200.0, 200.0);
[self.view addSubview:userInterfaceController.view];
}
The above code is drawing the context in my UserInterfaceViewController view in the mentioned frame which I am expecting.HelloGLKitViewController is having code to rotate cube in GLKViewControllerDelegate method "update" as mntioned below.but GLKViewControllerDelegate is not getting called when HelloGLKitViewController view to my UserInterfaceViewController view.
#import <GLKit/GLKit.h>
#interface HelloGLKitViewController : GLKViewController
#end
#implementation HelloGLKitViewController
#pragma mark - GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
//glClearColor(_curRed, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(_vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
#pragma mark - GLKViewControllerDelegate
- (void)update {
if (_increasing) {
_curRed += 1.0 * self.timeSinceLastUpdate;
} else {
_curRed -= 1.0 * self.timeSinceLastUpdate;
}
if (_curRed >= 1.0) {
_curRed = 1.0;
_increasing = NO;
}
if (_curRed <= 0.0) {
_curRed = 0.0;
_increasing = YES;
}
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
_rotation += -90 * self.timeSinceLastUpdate;//90 clockwise -90 anticlickwise
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(0), 1, 0, 0);//GLKMathDegreesToRadians(25) for bending Cube
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_rotation), 0, 1, 0);
// modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, self.view.frame.origin.x, self.view.frame.origin.y,0);
// modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -self.view.bounds.size.width/2, -self.view.bounds.size.height/2,0);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}
#end
What should I do to call GLKViewControllerDelegate update method.

Problem solved: Here is my code
#class HelloGLKitViewController;
#interface UserInterfaceViewController : UIViewController
{
HelloGLKitViewController *myHLKViewController;
}
#end
#implementation UserInterfaceViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"viewDidLoad of userinterfaceUiViewController");
myHLKViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"HelloGLKitViewController"];
[self.view addSubview:myHLKViewController.view];
[self addChildViewController:myHLKViewController];
[myHLKViewController didMoveToParentViewController:self];
}
#end
Thanks for the ref link Using GLKView with a UIViewController

Related

Draw line in UITextView

I want to draw line in my UITextView. After some research I found this:
UITextView ruled line background but wrong line height
I tried in my code, drawRect is called but no line is drawn.. Someone could help me out here ?
#import "FacebookViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface MyViewController (){
UITextView *text;
}
#end
#implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Do any additionnal customisation
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
text.contentMode = UIViewContentModeRedraw;
//TextView init
text = [[UITextView alloc]initWithFrame:CGRectMake(0,44,320,380)];
[self.view addSubview:text];
text.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)drawRect:(CGRect)rect {
NSLog(#"TEST");
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (text.contentSize.height + text.bounds.size.height) / text.font.leading;
//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
CGFloat baselineOffset = 6.0f;
//iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, text.bounds.origin.x, text.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, text.bounds.size.width, text.font.leading*x + 0.5f + baselineOffset);
}
CGContextClosePath(context);
CGContextStrokePath(context);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[text setNeedsDisplay];
}
#end
Probably a silly mistake but I can't find it out
Thanks
As i looked to you code everything is ok with it. Maybe there is something with color? May be it is the same as background color or something like this.
May be you forgot to set content mode:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
EDIT:
I think i know:
You forgot to set [myTextView setNeedsDisplay]; after showing it. Read here
EDIT:
First you create your view:
t = [[[MyTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)] autorelease];
[self.view addSubview:t];
t.delegate = self;
Make your viewcontroller implement UITextViewDelegate and after that
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[t setNeedsDisplay];
}
This should work
I have same problem ..
As i think you are not subclassing from UITextView
As UIViewController does not implement this method only UITextView did
So just subclass it .. you code will work
Is it possible that you're drawing successfully but getting obscured by a matching background color? I notice that the alpha on the color you've chosen is pretty small. If only to rule this out, change:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.5f alpha:0.15f].CGColor);
to
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

How to let drawRect get the value of points from ViewController and draw?

I created a class newView from UIView and aim to draw a quadrangle. the 4 coners (ClipPointA, ClipPointB, ClipPointC, ClipPointD) of the quadrangle are supposed to read value from ViewController.
newView.h:
#interface newView : UIView {
CGPoint ClipPointA, ClipPointB, ClipPointC, ClipPointD;
}
#end
newView.m:
#implementation newView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, ClipPointA.x, ClipPointA.y); //start point
CGContextAddLineToPoint(context, ClipPointB.x, ClipPointB.y);
CGContextAddLineToPoint(context, ClipPointD.x, ClipPointD.y);
CGContextAddLineToPoint(context, ClipPointC.x, ClipPointC.y); // end path
CGContextClosePath(context); // close path
CGContextSetLineWidth(context, 2.0);
CGContextStrokePath(context);
}
- (void)dealloc {
[super dealloc];
}
#end
I created the instance of newView in ViewController.
How do I write next part of code to let 4 CGPoints read value from ViewController?
Thanks.
when you create the new UIView please pass the points also for eg:let A,B,C,D be your 4 points
newView *nv =[[dragman alloc]initWithFrame:self.view.frame];
nv.ClipPointA=A;
nv.ClipPointB=B;
nv.ClipPointC=C;
nv.ClipPointD=D;
[nv setUserInteractionEnabled:YES];
[contentView addSubview:nv];
[nv release];

How to convert UIView as UIImage?

I am taking screenshot image from currentview.but i want to set frameā€¦.don't want to convert fullview as image.I gave frame size..but it takes always
Image as fullview..any help please?
CGRect rectt = CGRectMake(100, 150,150,200);
UIGraphicsBeginImageContext(rectt.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithFrame:rectt];
try to use this subClass:
// code to use the capture:
// in .h : #import "CaptureView.h"
CaptureView *cloneView = [[CaptureView alloc] initWithView:scrollView ];
[scrollView addSubview:cloneView];
with this files code
for CaptureView.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CALayer.h>
#interface CaptureView : UIView {
#private
UIImage *_imageCapture;
}
#property(nonatomic, retain) UIImage *imageCapture;
// Init
- (id)initWithView:(UIView *)view;
#end
for CaptureView.m:
#import "CaptureView.h"
// Private
#interface CaptureView (/* Private */)
- (void)settingImageFromView:(UIView *)view;
#end
// Public
#implementation CaptureView
#synthesize imageCapture = _imageCapture;
// Standard
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code.
}
return self;
}
// Init
- (id)initWithView:(UIView *)view {
// if ((self = [super initWithFrame:[view frame]])) {
if ((self = [super initWithFrame: CGRectMake(0, 0,150,200)])) {
// Initialization code.
[self settingImageFromView:view];
}
return self;
}
- (void)settingImageFromView:(UIView *)view {
// CGRect rect = [view bounds];
CGRect rect = CGRectMake(100, 150,150,200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *imageCaptureRect;
imageCaptureRect = UIGraphicsGetImageFromCurrentImageContext();
_imageCapture = imageCaptureRect;
// _imageCapture = UIGraphicsGetImageFromCurrentImageContext();
// [_imageCapture retain];
UIGraphicsEndImageContext();
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGPoint accPoint = CGPointMake(0,0);
[_imageCapture drawAtPoint:accPoint];
}
- (void)dealloc {
[_imageCapture release];
[super dealloc];
}
#end
quartz might help you... look at creating image from part of image....
its not a direct solution to your question, but i think you'll be able to tweak it to fit your needs..

UITextView ruled line background but wrong line height

I have a UITextView where the user can create notes and save into a plist file.
I want to be able to show lines just like a normal notebook. The problem I have is
that the text won't align properly.
The image below explains the problem quite well.
This is the background I use to create the lines like the Notes.app
This is my code for creating the background for my UITextView:
textView.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:19.0];
textView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"Notes.png"]];
I know that the UIFont.lineHeight property is only available in > iOS 4.x.
So I wonder if there is another solution to my problem?
You should try and draw your lines programmatically rather than using an image. Here's some sample code of how you could accomplish that. You can subclass UITextView and override it's drawRect: method.
NoteView.h
#import <UIKit/UIKit.h>
#interface NoteView : UITextView <UITextViewDelegate> {
}
#end
NoteView.m
#import "NoteView.h"
#implementation NoteView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
self.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:19];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
CGFloat baselineOffset = 6.0f;
//iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
#end
MyViewController.h
#import <UIKit/UIKit.h>
#import "NoteView.h"
#interface MyViewController : UIViewController <UITextViewDelegate> {
NoteView *note;
}
#property (nonatomic, retain) NoteView *note;
#end
MyViewController.m
#import "MyViewController.h"
#import "NoteView.h"
#define KEYBOARD_HEIGHT 216
#implementation MyViewController
#synthesize note;
- (void)loadView {
[super loadView];
self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease];
[self.view addSubview:note];
note.delegate = self;
note.text = #"This is the first line.\nThis is the second line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\nThis is the ... line.\n";
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[note setNeedsDisplay];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect frame = self.view.bounds;
frame.size.height -= KEYBOARD_HEIGHT;
note.frame = frame;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
note.frame = self.view.bounds;
}
- (void)dealloc {
[note release];
[super dealloc];
}
Take a look at Apple's documentation for Managing the Keyboard, specifically "Moving Content That Is Located Under the Keyboard". It explains how to listen for NSNotifcations and adjust your views properly.
I think the problem is with your image, the yellow space over the line is creating the problem.
You should edit the image.
And nice work.

How can I make a vertical UIToolbar?

How can I make a vertical UIToolbar?
Subclass UIToolbar and do the following:
CGFloat DegreesToRadian(CGFloat degrees)
{
return ((M_PI * (degrees))/ 180.0);
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.transform = CGAffineTransformMakeRotation(DegreesToRadian(90));
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView * subView in self.subviews)
{
if(!CGRectEqualToRect(subView.bounds, self.bounds))
subView.transform = CGAffineTransformMakeRotation(DegreesToRadian(-90));
}
}
Try this
#define M_PI 3.141
UIToolbar *tool;
tool.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 270.0/180*M_PI);