PageMove Using Touch

This is the PageMove application. In this application we will see, how to page move  using touch function.

Step 1: Create a Window base application using template. Give the application name  ”MovePage”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand classes and notice Interface Builder created the  MovePageAppDelegate  class for you. Expand Resources and notice the template generated a separate nib, MainWindow.xib, for the “MovePage”.

Step 4: In this project we need add to two UIView class, so now select classes -> Add -> NewFile -> Cocoa Touch Class -> Objective-C class -> and select UIView from the Subclass of. After that select next and Give the file name “PageView”. Do it once again and and give the another file name “PDFView”.

Step 5: We need add QuartzCore.framework in the framework folder. Select frameworks -> Add -> Existing Frameworks -> and select QuartzCore.framework.

Step 6: Another thing we have add in the project, i.e one pdf file, so add pdf file in the Resource folder. Give the pdf name “monsooninfo.pdf”.

Step 7: Now open the PDFView.h file and add the NSUInteger, CGPDFDocumentRef class. So make the following changes.

#import <UIKit/UIKit.h>

@interface PDFView : UIView {

        NSUInteger PDFNumber;
        CGPDFDocumentRef pdf;
}

@property (assign) NSUInteger PDFNumber;

Step 8: Open the PDFView.m file and make the following changes.

(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
                CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("monsooninfo.pdf"), NULL, NULL);
                pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
                CFRelease(pdfURL);
                self.PDFNumber = 1;
                self.backgroundColor = nil;
                self.opaque = NO;
                self.userInteractionEnabled = NO;
    }
    return self;
}

(void)drawRect:(CGRect)rect {
 
       
        CGContextRef context = UIGraphicsGetCurrentContext();
       
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, PDFNumber);
       
        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1, 1);
       

        CGContextSaveGState(context);
        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, self.bounds, 0, true);
        CGContextConcatCTM(context, pdfTransform);
        CGContextDrawPDFPage(context, page);
        CGContextRestoreGState(context);
}

Step 9: Open the PageView.h file and make the following changes.

#import <UIKit/UIKit.h>
#import "PDFView.h"

@interface PageView : UIView {
        NSUInteger PDFNumber;
       
@private
        PageView *View1;
        PageView *View2;
        PageView *View3;
       
}
@property (assign) NSUInteger PDFNumber;

Step 10: Now double click MainWindow.xib file and open it to the Interface Builder. First drag the view from the library and place it to the window. Select the view icon from the mainwindow and bring up IdentityInspector and select the pageView class (See the figure 1). Now save it, close it and go back to the Xcode.

Figure 1: Interface Builder connection

Step 11: Now open the PDFView.m file and make the following changes.

(void)awakeFromNib
{
       
  View1 = [[PDFView alloc] initWithFrame:self.bounds];
        View2 = [[PDFView alloc] initWithFrame:self.bounds];
       
        self.backgroundColor = [UIColor blackColor];
       
        [self addSubview:View3];
       
        View3.PDFNumber = 2;
        [self addSubview:View1];
}

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        lastPos = [[touches anyObject] locationInView:self];
}

CGPoint vectorBetweenPoints(CGPoint firstPoint, CGPoint secondPoint) {
        CGFloat xDifference = firstPoint.x secondPoint.x;
        CGFloat yDifference = firstPoint.y secondPoint.y;
       
        CGPoint result = CGPointMake(xDifference, yDifference);
       
        return result;
}

CGFloat distanceBetweenPoints(CGPoint firstPoint, CGPoint secondPoint) {
        CGFloat distance;
       
                CGFloat xDifferenceSquared = pow(firstPoint.x secondPoint.x, 2);
               
                CGFloat yDifferenceSquared = pow(firstPoint.y secondPoint.y, 2);
       
                distance = sqrt(xDifferenceSquared + yDifferenceSquared);
        return distance;
       
}
CGFloat angleBetweenCGPoints(CGPoint firstPoint, CGPoint secondPoint)
{
        CGPoint previousDifference = vectorBetweenPoints(firstPoint, secondPoint);
        CGFloat xDifferencePrevious = previousDifference.x;
       
        CGFloat previousDistance = distanceBetweenPoints(firstPoint,
                                                                                                         secondPoint);
        CGFloat previousRotation = acosf(xDifferencePrevious / previousDistance);
       
        return previousRotation;
}

(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
        CGPoint currentPos = [[touches anyObject] locationInView:self];
       
        fingerDelta = distanceBetweenPoints(currentPos, lastPos)/2;
       
        CGPoint fingerVector = vectorBetweenPoints(currentPos, lastPos);
       
       
       
        if ([[filter valueForKey:@"inputTime"] floatValue] < 0.9)
        {      
                [self bringSubviewToFront:View1];
                [filter release];
                filter = nil;
                filter = [[CAFilter filterWithType:kCAFilterPageCurl] retain];
                [filter setDefaults];
                [filter setValue:[NSNumber numberWithFloat:((NSUInteger)fingerDelta)/100.0] forKey:@"inputTime"];
               
                CGFloat _angleRad = angleBetweenCGPoints(currentPos, lastPos);
                CGFloat _angle = _angleRad*180/M_PI ;          
                if (_angle < 180 && _angle > 120)
                {
                        if (fingerVector.y > 0)
                                [filter setValue:[NSNumber numberWithFloat:_angleRad] forKey:@"inputAngle"];
                        else
                                [filter setValue:[NSNumber numberWithFloat:-_angleRad] forKey:@"inputAngle"];
                       
                        View1.layer.filters = [NSArray arrayWithObject:filter];
                }
        }
}

(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{      
        if ([[filter valueForKey:@"inputTime"] floatValue] > 0.7)
        {
                View1.PDFNumber ++;
                [View1 setNeedsDisplay];
               
                View3.PDFNumber ++;
                [View3 setNeedsDisplay];
               
                View1.layer.filters = nil;
               
                [filter setValue:[NSNumber numberWithFloat:0.0] forKey:@"inputTime"];
               
               
                CAFilter *previousFilter = [[CAFilter filterWithType:kCAFilterPageCurl] retain];
                [previousFilter setDefaults];
                [previousFilter setValue:[NSNumber numberWithFloat:0.91] forKey:@"inputTime"];
               
                [previousFilter setValue:[NSNumber numberWithFloat: M_PI] forKey:@"inputAngle"];
        }
        else
        {              
               
        }
}

Step 12: Now compile and run the application in the simulator. See the figure 2

Figure 2: Output of the Example.

You can Download SourceCode from here MovePage

Leave a Reply

Your email address will not be published. Required fields are marked *