ios - Where to place UIView animation code in VIPER? -
where should uiview animation code placed according viper design pattern?
should in view or in presenter?
note:
i have customview
wish move across screen once touched.
the customview
added screen in viewcontroller
you should place in [r]outer. it's described in this article:
since presenter contains logic react user inputs, presenter knows when navigate screen, , screen navigate to. meanwhile, wireframe knows how navigate. so, presenter use wireframe perform navigation. together, describe route 1 screen next.
the wireframe obvious place handle navigation transition animations.
note called router wireframe.
update (answer corrections based on updates of question , comments)
i’ll give opinion based on these articles. lets consider 2 cases: simple , more sophisticated. in simple case task change view's position predefined 1 animation(i.e. change state). , in more sophisticated case task change position of custom view based on coordinates of touch.
take @ simple case. have [v]iewcontroller contains custom view. viewcontroller adopts viperview protocol:
@protocol viperview - (void)changecustomviewstate; @end @interface viewcontroller : uiviewcontroller<viperview> @end
in implementation have this:
@implementation viewcontroller { bool _isinitialstate; iboutlet uiview *_customview; } - (void)changecustomviewstate { _isinitialstate = !_isinitialstate; [self changecustomviewpositionanimated:yes]; } - (void)changecustomviewpositionanimated:(bool)animated { void(^performchange)() = ^() { if (_isinitialstate) _customview.center = cgpointmake(50, 50); else _customview.center = cgpointmake(250, 250); }; if (animated) { [uiview animatewithduration:[catransaction animationduration] animations:^{ performchange(); }]; } else { performchange(); } }
as view's responsibilities according viper "display information user" , "detect user interaction” , isn't allowed make decisions after touch except notify [p]resenter event. presenter in turn make decision , calls
[self.viperview changecustomviewstate];
thus actual code performs animation located in [v]iew [p]resenter triggers execution (because responsibilities "tell view display" , "handle events"). defining position of custom view jsut part of view’s layout. it’s part of configuration. presenter turns in animated way.
in more sophisticated case we’ll consider changing of custom view’s position depending on touch location. our task change position of view after touch in such way remains on screen. example if view located in bottom left angle of screen, touch shouldn’t move below screen's bounds or behind left side of screen. should move view in 1 of 3 free corners. in case our protocol view looks this:
@protocol viperview // @param - related position screen bounds in range (0;1) - (void)changecustomviewrelatedposition:(cgpoint)point animated:(bool)animated; @end
and in implementation
@implementation viewcontroller { iboutlet uiview *_customview; } - (void)changecustomviewrelatedposition:(cgpoint)point animated:(bool)animated { cgpoint thiscoordinatespacepoint = // translate ‘point’ screen’s coordinate space ; void(^performchange)() = ^() { _customview.center = thiscoordinatespacepoint; }; if (animated) { [uiview animatewithduration:[catransaction animationduration] animations:^{ performchange(); }]; } else { performchange(); } } - (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event { cgpoint point = //determine location [self.viperpresenter userdidtouchviewwithposition:point]; } @end
for [p]resenter need define protocol in case:
@protocol viperpresenter - (void)userdidtouchviewwithposition:(cgpoint)point; @end
when user touches screen view calls presenter notify event:
[self.viperpresenter userdidtouchviewwithposition:point];
as article states:
the presenter notified of events view , part of job handle events accordingly. means asking interactor retrieve bit of information or carry out task.
in our case app needs determine coordinates view should moved. algorithm encapsulated , interchangeable. retrieve algorithm different places: database, server, etc. accomplish task use [i]nteractor:
// in presenter class [self.viperinteractor handlecoordinates:(cgpoint)point];
then [i]nteractor asks datamanager map these coordinates new ones somehow using algorithm [e]ntity (should moved in upper right or upper left corner) , notifies [p]resenter new coordinates(the corner view should move). , presenter performs:
[self.viperview changecustomviewrelatedposition:newposition];
again, animation code placed inside [v]iew part of layout. decision(exact parameters) defined other components(presenter, interactor, entity)
Comments
Post a Comment