iOS Reader Practice Series (iv) Reading view scenarios

Source: Internet
Author: User

The article content may need a lot of screen to show up during reading, so the question now is how many views to create to render the content.

After consideration, it is decided to use two views, using gestures to control the continuous switching back and forth to achieve.

First, define the view object for the two private entities:

@property (nonatomic, Strong) Displymanagerview *buffer1; @property (nonatomic, strong) Displymanagerview *buffer2; 

Buffer1 and Buffer2 are the two views used to render content.

Next, define a view to distinguish between the currently displayed view or the one that is overwritten by the current view (for the next or previous page rendering):

@property (nonatomic, Strong) Displymanagerview *Curdisplayview; @property (nonatomic, strong) Displymanagerview * Nextdisplayview;

Curdisplayview and Nextdisplayview are just two references to Buffer1 and buffer2, not a view of the two entities actually added to the upper view. They only distinguish between the current view and the next view to be displayed. Maybe this looks a bit around, the code below is easy to understand.

Implementation phase when the parent view is initialized, you create two entity views and assign values to two indexed views and add the appropriate gestures:

-(ID) initWithFrame: (cgrect) frame{    if (self = [Super Initwithframe:frame])    {         [self Createsubviews];        [Self addpangesture];    }    return self ;}  

If you want to go through other initialization methods when initializing, add them to the other initialization methods.

-(void) createsubviews{    _buffer2 = [[Displymanagerview alloc] initWithFrame:self.bounds];    [_buffer2 Createbottomview];    Typographicmanager *amanager = [Typographicmanager getinstanse];    [_buffer2 SetBgColor:aManager.backgroundColor];    [Self addsubview:_buffer2];    _nextdisplayview = _buffer2;        _buffer1 = [[Displymanagerview alloc] initWithFrame:self.bounds];    [_buffer1 Createbottomview];    [_buffer1 SetBgColor:aManager.backgroundColor];    [Self addsubview:_buffer1];    _curdisplayview = _buffer1;}      
-(void) addpangesture{    self.userinteractionenabled = YES;    Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)];    Pan.delegate = self ;    [Self addgesturerecognizer:pan];        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)];    Tap.delegate = self ; [Self Addgesturerecognizer:tap];}     

Here we only explain the corresponding method of the swipe gesture:-(void) pan: (Uipangesturerecognizer *) panges{

Cgpoint pointer =[Panges locationinview:self]; if (panges.state = =Uigesturerecognizerstatebegan)//sliding start {_startx =pointer.x; _SCROLLINGX =pointer.x; _DISPLYVIEWX =_curdisplayview.frame.origin.x; } else if (panges.state = =uigesturerecognizerstatechanged)//during sliding (the condition is always in this state during sliding, that is, each time the method is called during the slide process) {Cgpoint pointer =[Panges locationinview:self]; if (Pointer.x >_SCROLLINGX) {_instantdirection =Scrollinginstantdirectionright; } else if (pointer.x = =_SCROLLINGX) {_instantdirection =Scrollinginstantdirectionnone; } else{_instantdirection =Scrollinginstantdirectionleft; } _SCROLLINGX =pointer.x; Record the instantaneous touch point coordinates if (self.delegate! = Nil && [self.delegate respondstoselector: @selector (typographicviewbeginscroll)]) {[Self.delegate  typographicviewbeginscroll];} 

//The following refers to the way the page is used, using the decorative mode Typographicviewflipovermanager *flipovermanager = Typographicviewflipovermanager.instance; FlipOverManager.decorateView.typographicView = self; [Flipovermanager displyviewmovetox:pointer.x- _startx];} else if (panges.state = = uigesturerecognizerstateended)//sliding End {if (pointer.x-_startx > 0 ) {if (_instan Tdirection = = scrollinginstantdirectionleft) {[Self resetview:pointer.x- _startx isright:no];} else {[Self resetview:pointer.x- _startx isright:yes];}} else {if (_instantdirection = = scrollinginstantdirectionright) {[Self resetview:pointer.x- _startx I Sright:yes]; } else {[Self resetview:pointer.x- _startx isright:no];}}}

The method mainly consists of three stages: sliding start, sliding process, sliding end.

The start phase of the slide begins with the coordinates of the start touch point.

During the sliding process, the coordinates of the instantaneous contact point in the sliding process are recorded, and the sliding direction is judged according to the instantaneous coordinates, and the view is moved in real time according to the coordinates of the instantaneous contact point. This process will involve a different way of paging, two views will have a different way of moving, the use of decorative mode, compared to the use of inheritance to achieve this function, the use of decorative mode of this combination of more flexible way.

Slide the end phase and primarily determine the final position of the view:

-(void) Resetview: (cgfloat) MoveX isright: (BOOL) isright{    cgfloat width = self.frame.size.width;    Typographicviewflipovermanager *flipovermanager = typographicviewflipovermanager.instance;    FlipOverManager.decorateView.typographicView = self ;    if (MoveX <=-width/100)    {        if (isright && moveX >-width/3) {[Flipovermanager Recover Page:movex]; } else {[Flipovermanager movetonextpage];}} else if (MoveX >= width/100) {if (!isright && MoveX < WIDTH/3) {[Flipovermanager Recoverpage:movex];} else {[Flipovermanager movetoprepage];}} else /c16> 
Methods in the Flipovermanager object:
    Func Displyviewmovetox (_ X:cgfloat) {        _ = Self.decorateview?. Displyviewmoveto (x:x)    }        func movetonextpage () {        self.decorateview?. Movetonextpage ()    }        func movetoprepage () {        self.decorateview?. Movetoprepage ()    }        func Recoverpage (_ movex:cgfloat) {        self.decorateview?. Recoverpage (MoveX)    }   
methods in the Decorateview object:
Class Typographicdecorateview:typographicextensionview {        var typographicview:typographicview?        Override func Displyviewmoveto (x:cgfloat),Bool {let        x = x                return (Self.typographicview?. Displyviewmoveto (x:x))!     }        override func movetonextpage () {                self.typographicview?. Movetonextpage ()} Override func Movetoprepage () {Self.typographicview?. Movetoprepage ()} Override Func Recoverpage (_ Movex:cgfloat) {}}        
Typographicview is the parent view of the Buffer1 and Buffer2, which is said earlier, and the corresponding code in Typographicview is:
-(void) Recoverpage: (cgfloat) movex{}-(void) movetonextpage{    [self switchbuffer];    [_readmanager Changeviewindextonext];   Processing data changes}-(void) movetoprepage{    [self switchbuffer];    [_readmanager Changeviewindextopre];   Handling changes in data  


-(void) Switchbuffer //This method toggles the current and next view to be displayed as a result of sliding

If

_curdisplayview =
_nextdisplayview =
_isfirstviewdisplay =

Else

_curdisplayview =
_nextdisplayview =
_isfirstviewdisplay =




-
{
  Some business data on the judgment to return to yes/no, no need to care
}

This series of processes embodies the idea of a decorative pattern, which may be a bit dizzy without explanation.

First write this, this article is not finished to be continued ...



iOS Reader Practice Series (iv) Reading view scenarios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.