What are the rules for PIC change during flight?
@ymb1 is correct in that it does not matter who the legal pilot in command is per §1.1.  What does matter is following the rules in §61.51(e).


  (e) Logging pilot-in-command flight time. (1) A sport, recreational, private, commercial, or airline transport pilot may log pilot in command flight time for flights-
  
  
    (i) When the pilot is the sole manipulator of the controls of an aircraft for which the pilot is rated, or has sport pilot privileges for that category and class of aircraft, if the aircraft class rating is appropriate;
    
    (ii) When the pilot is the sole occupant in the aircraft;
    
    (iii) When the pilot, except for a holder of a sport or recreational pilot certificate, acts as pilot in command of an aircraft for which more than one pilot is required under the type certification of the aircraft or the regulations under which the flight is conducted; or
    
    (iv) When the pilot performs the duties of pilot in command while under the supervision of a qualified pilot in command provided—
  


If in your example both pilots are rated to fly that airplane on your license whether or not you have the required endorsements or ratings to act as the legal pilot in command you can log PIC time.  It does not matter who the legal pilot in command is at that time.  Both pilots will log the flight time as PIC and total time for the time you are the sole manipulator of the controls.



Other ways both pilots can log PIC time is where one pilot who is the safety pilot is the legal pilot in command while the other pilot who is flying the airplane and sole manipulator of controls is under simulated instrument flight.

Let me be very clear here.  If both pilots were to log PIC time under this condition, and many pilots do, they are making the statement in their logbook that could get them in trouble later on.  Let me explain, pilot A and B go out and fly together; Pilot A is under the hood and the other is the legal pilot in command.  On another day, Pilot B is under the hood and the other is the legal pilot in command.  Both pilots log exactly the same amount of flight time for both PIC and total time on these flights.  At this point, a precedence is set. 

What is that statement you are making in your logbook?
Who the legal pilot in command is for each flight.  Read the regulation again.  If the safety pilot logs PIC time they are accepting legal pilot in command responsibilities and all that goes with it.  If the pilot under the hood has an incident or accident or airspace infraction the FAA will want to know who the legal pilot in command was during that flight.  The logbooks tell the story regardless what the pilots say at that moment.